package o2o.web.actions.pay;


//订单支付

import com.google.gson.Gson;
import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.JYListSort;
import com.jinyou.utils.common.JYMathDoubleUtils;
import com.jinyou.utils.common.ValidateUtil;
import com.jinyouapp.common.tools.jms.model.JMSNotify;
import jPush.dao.JPushMessage;
import map.HttpRequest;
import notice.dao.NoticeType;
import o2o.business.sms.JYSmsSeverBusiness;
import o2o.business.sms.data.SMS_TYPE;
import o2o.cache.agent.CityAgentMemory;
import o2o.cache.company.postman.PostCompanyMemory;
import o2o.cache.order.*;
import o2o.cache.postman.CityPostmanMemory;
import o2o.cache.shop.ShopMemory;
import o2o.cache.sys.SysVariableDistributeMemory;
import o2o.cache.sys.SysVariableScoreLevelMemory;
import o2o.cache.wallet.UserWalletFlowMemory;
import o2o.config.dao.UserDaoSql;
import o2o.config.jms.config.JMSTypeContent;
import o2o.config.jms.sender.JmsSender;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.agent.CityAgent;
import o2o.dao.PO.common.ResultInfo;
import o2o.dao.PO.common.UserType;
import o2o.dao.PO.company.postman.PostCompanyInfo;
import o2o.dao.PO.device.door.DeviceCompanyContact;
import o2o.dao.PO.order.*;
import o2o.postman.model.CityPostman;
import o2o.dao.PO.score.UserScoreFlow;
import o2o.company.model.ShopInfo;
import o2o.dao.PO.sys.SysVariableDistribute;
import o2o.dao.PO.sys.SysVariableScoreLevel;
import o2o.dao.PO.wallet.PAY_TYPE;
import o2o.dao.PO.wallet.UserWalletFlow;
import o2o.dao.PO.wallet.UserWalletFlowType;
import o2o.service.company.postman.PostManSalaryService;
import o2o.system.data.SYS_CONFIG_PATH;
import o2o.system.data.SYS_SETTINGS;
import o2o.task.jPush.JPushTask;
import o2o.task.order.OrderTaskUtil;
import o2o.web.actions.notice.Notice;
import o2o.web.actions.order.OrderMan;
import o2o.web.actions.order.SubmitOrderMan;
import o2o.web.actions.order.shop.ShopOrderController;
import o2o.web.actions.order.thread.CancelOrder4ShopThread;
import o2o.web.actions.wallet.UserWalletMan;
import org.apache.commons.lang.StringUtils;
import pay.ChannelUtils;
import pay.PayUtils;
import pay.TradeStatusUtils;
import pay.dao.QueryResult;
import pay.dao.WX_ALI_TRI_CHANNEL_TYPE;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;

public class OrderPay {

    public static final String POSTMAN_YJ = "postman";// 押金

    //订单回调业务处理
    public static ResultInfo userOrderPayOver(String sysAppKey,
                                              String payType, String orderNo, String chargeId) {
        String appFlag = "";
        if (ValidateUtil.isNotNull(orderNo)) {
            OrderInfo orderInfo = OrderInfoMemory.getInstance().getOrderInfo(orderNo);
            if (null != orderInfo) {
                appFlag = orderInfo.getPayTypeFlag();
            }
        }
        return orderPayOver(sysAppKey, appFlag,
                payType, UserType.USER, orderNo, chargeId, orderNo, true);
    }

    public static ResultInfo userOrderPayOver(String sysAppKey, String appFlag,
                                              String payType, String orderNo, String chargeId) {
        return orderPayOver(sysAppKey, appFlag,
                payType, UserType.USER, orderNo, chargeId, orderNo, true);
    }


    //订单回调业务处理
    public static ResultInfo userOrderPayOver(String sysAppKey, String appFlag,
                                              String payType,
                                              String orderNo,
                                              String chargeId,
                                              String payOrderNo,
                                              Boolean needQueryTHIStatus//是否需要查询第三方支付平台状态
    ) {
        return orderPayOver(sysAppKey, appFlag,
                payType, UserType.USER, orderNo, chargeId, payOrderNo, needQueryTHIStatus);
    }

    // 正在处理中
    private static HashSet<String> orderSet = new HashSet<>();

    public static ResultInfo orderPayOver(String sysAppKey, String appFlag,
                                          String payType, Integer userType, String orderNo,
                                          String chargeId,
                                          String payOrderNo,
                                          Boolean needQueryTHIStatus//是否需要查询第三方支付平台状态
//                                          Boolean savePayType// 是否需要保存支付方式（）
    ) {
        //获取订单状态
        OrderInfo orderInfo = OrderInfoMemory.getInstance().getOrderInfo(orderNo);
        if (null == orderInfo) {
            return new ResultInfo("当前订单不存在，请稍后重试");
        }
        if (OrderMan.isMultipleSonOrder(orderInfo)) {
            payOrderNo = orderInfo.getPayOrderNo();
        }
//        boolean isArrivePay = false;
//        if (orderInfo.getIsArrivePay() != null && 0 == 1 - orderInfo.getIsArrivePay()) {
//            isArrivePay = true;
//        }
        //订单已取消的就不要在处理了
        if ((null != orderInfo.getOrderType() && 0 != ORDER_TYPE.SERVICE_LOCK - orderInfo.getOrderType()
                && 0 != ORDER_STATUS.SUBMIT - orderInfo.getOrderStatus() && 0 != ORDER_STATUS.CANCEL_BY_SYSTEM - orderInfo.getOrderStatus()
                && 0 != ORDER_STATUS.UNDERLINE_SUBMIT - orderInfo.getOrderStatus()
                && 0 != ORDER_STATUS.ARRIVE_PAY - orderInfo.getOrderStatus()
        ) || (null != orderInfo.getOrderType() && 0 == ORDER_TYPE.SERVICE_LOCK - orderInfo.getOrderStatus() && 0 != ORDER_STATUS.POSTMAN_FINISH - orderInfo.getOrderStatus())
        ) {
            return new ResultInfo();
        }
        String username = orderInfo.getUsername();//用户信息

        //获取店铺信息
        ShopInfo shopInfo = null;
        //支付时检测店铺是否营业中  1检测  0不检测
        if (OrderMan.isMultipleParentOrder(orderInfo)) {
            // 多商家订单（主订单）
        } else {
            if (null != SYS_SETTINGS.PAY_CHECK_SHOP_STATUS && 0 == SYS_SETTINGS.PAY_CHECK_SHOP_STATUS) {
                // 不检测店铺是否在营业中
            } else {
                //检测店铺是否在营业
                if (null != orderInfo.getOrderType()
                        && (0 == ORDER_TYPE.SHOP_GOODS - orderInfo.getOrderType() //商城订单
                        || 0 == ORDER_TYPE.SHARE_GOODS_ORDER - orderInfo.getOrderType() //分享商品订单（爱甩卖）
                        || 0 == ORDER_TYPE.GROUP_ORDER - orderInfo.getOrderType() // 团购订单
                        || 0 == ORDER_TYPE.GROUP_BUY - orderInfo.getOrderType())//拼团订单
                ) {
                    shopInfo = ShopMemory.getInstance().getShopById(orderInfo.getShopId());
                    if (null == shopInfo) {
                        return new ResultInfo("获取店铺信息失败");
                    }
                }
            }
        }

        boolean isPostmanJY = false;  // 是不是骑手端缴纳押金的 是的话则查询骑手的
        if (orderInfo.getOrderType() != null
                && orderInfo.getOrderType() == ORDER_TYPE.WALLET_DEPOSIT_IN
                && ValidateUtil.isNotNull(orderInfo.getExtra3())
                && POSTMAN_YJ.equals(orderInfo.getExtra3())) {
            isPostmanJY = true;
        }

        if (ValidateUtil.isNull(payType)) {
            payType = orderInfo.getPayType();
        }

        if (null != payType) {
            //ChannelUtils.paypal.equals(payType) ||
            if (ChannelUtils.transit.equals(payType) || ChannelUtils.stripe.equalsIgnoreCase(payType)) {
                needQueryTHIStatus = false;
            }
        }


        boolean hasPay = OrderPayMemory.getInstance().hasPay(orderNo);
        if (hasPay) { //流水中有，已经完成支付
            if (0 == 1 - orderInfo.getIsPaid()) { //订单已经成功支付了,结束

            } else { //订单状态还未变，改变订单状态
                StringBuffer payType2Str = new StringBuffer();
                String chargeStr = "";
                //是否需要查询第三方支付平台支付状态
                if (null != needQueryTHIStatus && !needQueryTHIStatus) {
                    //不需要查询（主要是paypal）
                    if (ValidateUtil.isNotNull(chargeId)) {
                        // stripe 传入了chargeId
                        chargeStr = ",chargeId = '" + chargeId + "'";
                    }
                } else {
                    //                    ResultInfo resultInfo = getTradeStatusQ(payType, payOrderNo, null);
                    ResultInfo resultInfo = null;
                    if (orderInfo.getIsFreeGoodOrder() != null && orderInfo.getIsFreeGoodOrder() == 1) {
                        resultInfo = new ResultInfo();
                        QueryResult queryResult = new QueryResult();
                        resultInfo.setSuccess(true);
                        resultInfo.setObj(queryResult);
                    } else if (isPostmanJY) {
                        // 骑手的
                        resultInfo = getQsTradeStatusQ(payType, payOrderNo, null);
                    } else {
                        System.out.println("查询支付：" + orderNo + ",ChargeId:" + orderInfo.getChargeId() + ",payType:" + payType);
                        resultInfo = getTradeStatusQ(orderInfo.getAgentId(), appFlag,
                                payType, userType, payOrderNo, orderInfo.getChargeId());
                    }
                    if (null == resultInfo) {
                        return new ResultInfo("请稍后重试");
                    } else {
                        if (!resultInfo.isSuccess()) {//失败，返回错误
                            System.out.println("订单支付成功后，查询订单失败2");
                            return resultInfo;
                        }
                        try {
                            QueryResult queryResult = (QueryResult) resultInfo.getObj();
                            if (null != queryResult) {
                                if (queryResult.isQuerySuccess() && queryResult.isPaySuccess() && ValidateUtil.isNotNull(orderInfo.getPayType())) {
                                    switch (orderInfo.getPayType()) {
                                        case ChannelUtils.yiyi: {
                                            String payType2 = queryResult.getNote();
                                            String paySign = orderInfo.getPaySign() + ";" + queryResult.getMsg();
                                            payType2Str.append(",payType2 = '").append(payType2).append("',paySign='").append(paySign).append("'");
                                        }
                                        break;
                                        case ChannelUtils.alipay:
                                        case ChannelUtils.wx:
                                        case ChannelUtils.wx_xcx: {
                                            if (null != SYS_SETTINGS.WX_ALI_USE_TRI && 0 == WX_ALI_TRI_CHANNEL_TYPE.NIHAOPAY - SYS_SETTINGS.WX_ALI_USE_TRI) {
                                                String chargeId2 = queryResult.getNote();
//                                                payType2Str.append(",chargeId = '").append(chargeId2).append("'");
                                                chargeStr = ",chargeId = '" + chargeId2 + "'";
                                            }
                                        }
                                        break;
                                    }
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }

                //如果是自取的话生成取货验证码
                //如果是自取的话生成取货验证码
                String verifyCode = "";
                if (orderInfo.getOrderType() != null && orderInfo.getOrderType() != ORDER_TYPE.GROUP_ORDER) {
                    try {
                        if (0 == 1 - orderInfo.getIsZiQu()) {
                            verifyCode = ",verifyCode='" + OrderMan.getVerifyCodeDaodian() + "'";
                        } else if (SYS_SETTINGS.HAS_ORDER_SECOND_POSTMAN_VERIFY_CODE == 1 && (orderInfo.getShopSelfPost() != null && orderInfo.getShopSelfPost() == 0)) {
                            // 二级骑手验证码
                            verifyCode = ",verifyCode='" + OrderMan.getVerifyCode() + "'";
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (ValidateUtil.isNull(verifyCode)) {
                        verifyCode = "";
                    }
                }
                // 聚合订单没有提货码
                if (OrderMan.isMultipleParentOrder(orderInfo)) {
                    // 修改订单状态
                    // 聚合订单处理
                    ResultInfo resultInfo = modifyMultipleOrderStatus(orderNo, payType2Str);
                    if (resultInfo == null) {
                        System.out.println("聚合订单修改订单状态失败V1.1");
                        return new ResultInfo("更新订单状态失败，请稍后再试V1.1");
                    }
                    if (!resultInfo.isSuccess()) {
                        System.out.println(resultInfo.getError());
                        return new ResultInfo("更新订单状态失败，请稍后再试V1.1");
                    }
                } else {
                    // 不是聚合订单的
                    int orderSta = ORDER_STATUS.PAYED;
                    if (orderInfo.getOrderType() != null && orderInfo.getIsArrivePay() != null && orderInfo.getIsArrivePay() == 1) {
                        orderSta = ORDER_STATUS.ARRIVE_PAY;
                    } else if (orderInfo.getOrderType() != null && orderInfo.getOrderType() == ORDER_TYPE.GROUP_ORDER) {
                        orderSta = ORDER_STATUS.SHOP_SURE;
                    } else if (orderInfo.getOrderType() != null && (orderInfo.getOrderType() == ORDER_TYPE.SHOP_SERVICE || orderInfo.getOrderType() == ORDER_TYPE.SHOP_RESCUE)) {
                        orderSta = ORDER_STATUS.SHOP_SELECT;
                    }
                    // 获取该店铺的每日订单号
                    String orderPreNo = "";
                    if ((null == orderInfo.getPreDayNo() || 0 == orderInfo.getPreDayNo()) && null != orderInfo.getShopId() && null != orderInfo.getOrderType()) {
                        String shopOwn = "admin";
                        if (null != shopInfo) {
                            shopOwn = shopInfo.getOwnUsername();
                        }
                        int preDayNo = SubmitOrderMan.getShopPreDayNo(orderInfo.getShopId(), shopOwn, orderInfo.getUsername());
                        orderPreNo = ",preDayNo=" + preDayNo;
                    }

                    StringBuilder sb = new StringBuilder();
//                    sb.append("update ").append(PojoTables.OrderInfo).append(" set isPaid =1,orderStatus=").append(ORDER_STATUS.PAYED).append(verifyCode).append(" where delFlag =0 and  orderNo = '").append(orderNo).append("'");
//                     sb.append("update ").append(PojoTables.OrderInfo).append(" set isPaid =1,orderStatus=").append(orderSta).append(verifyCode).append(orderPreNo)
                    sb.append("update ").append(PojoTables.OrderInfo).append(" set ").append((orderSta - ORDER_STATUS.ARRIVE_PAY == 0 ? "" : " isPaid =1,"))
                            .append(" orderStatus=").append(orderSta).append(verifyCode).append(orderPreNo)
                            .append(payType2Str.toString())
                            .append(chargeStr)
                            .append(" where delFlag =0 and  orderNo = '").append(orderNo).append("'");
                    try {
                        UserDaoSql.getInstance().executeBySql(sb.toString());
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return new ResultInfo("更新订单状态失败，请稍后再试");
                    }
                    //更新缓存中订单状态
                    OrderInfoMemory.getInstance().paidModifyStatus(orderNo);
//                    OrderInfoMemory.getInstance().modifyStatus(orderNo);
                }
                //跑腿订单
                if (null != orderInfo.getOrderType() && (
                        0 == ORDER_TYPE.PAO_TUI - orderInfo.getOrderType()
                                || 0 == ORDER_TYPE.DAI_GOU - orderInfo.getOrderType()
                                || 0 == ORDER_TYPE.PERSON_TRANSPORT - orderInfo.getOrderType()
                                || 0 == ORDER_TYPE.BANG_BAN - orderInfo.getOrderType()
                                || 0 == ORDER_TYPE.HUO_YUN - orderInfo.getOrderType())) {//跑腿订单
                    StringBuilder sbOther = new StringBuilder();
                    sbOther.append("update ").append(PojoTables.OTHER_ORDER_INFO).append(" set isPaid =1,orderStatus=").append(ORDER_STATUS.PAYED).append(verifyCode).append(" where delFlag =0 and  orderNo = '").append(orderNo).append("'");
                    try {
                        UserDaoSql.getInstance().executeBySql(sbOther.toString());
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return new ResultInfo("更新订单状态失败，请稍后再试");
                    }

                    //缓存修改
                    OtherOrderMemory.getInstance().addOrModify(orderNo);
                    // 此处修改保险单的支付状态
                    if (null != orderInfo.getOrderType() && 0 == ORDER_TYPE.PERSON_TRANSPORT - orderInfo.getOrderType()) {
                        modifyOrderPolicyRecordPaidStatus(orderInfo, username);
                    }
                    // 订单模式 在抢单之前判断订单模式
                    if (SYS_SETTINGS.ADMIN_MANAGER_GRAB_ORDER_MODE == 0 && SYS_SETTINGS.GRAB_ORDER_MODE != null && GRAB_ORDER_MODE.ONLY_DISPATCH_MODE == SYS_SETTINGS.GRAB_ORDER_MODE) {
                        // 若是仅调度模式 不给骑手推送
                    } else {
                        // 推送给骑手
                        if (!OrderMan.isMultipleSonOrder(orderInfo)) {
                            JPushMessage jPushMessage = new JPushMessage(true, orderInfo.getShopCity(), "又有新的订单了", "又有新的订单了，快去抢单吧");
                            jPushMessage.setPushUserType(JPushMessage.POSTMAN);//推给配送员
                            jPushMessage.setJpSubType("");
                            JPushTask.jPushMessageList.add(jPushMessage);
                        }
                    }
                }

                // create by yangtk 2018/11/14
                // if (orderInfo.getIsArrivePay() != null && 0 == 1 - orderInfo.getIsArrivePay()) {
                //     // 货到付款订单 不在推送
                //
                // } else {
                // 非货到付款订单
                // 聚合订单 极光已推送问题
                if (OrderMan.isMultipleParentOrder(orderInfo)) {
                    multipleOrderNotice(sysAppKey, orderInfo);
                } else if (null != shopInfo) {

                    //极光推送
                    if (orderInfo.getOrderType() != null && orderInfo.getOrderType() != ORDER_TYPE.GROUP_ORDER) {
                        Notice.publishNotice2Person(sysAppKey, shopInfo.getOwnUsername(), JPushMessage.SHOP_OWNER, NoticeType.order, "又有新的订单了", "有新的订单，请尽快处理");
                    }
                    if (null != SYS_SETTINGS.SHOP_NEW_ORDER_SMS && 0 == 1 - SYS_SETTINGS.SHOP_NEW_ORDER_SMS) {
                        //发送短信
                        JYSmsSeverBusiness.sendSMS(sysAppKey, shopInfo.getTelephone(), SMS_TYPE.SHOP_NEW_ORDER);
                    }
                }
                // }
                // end
            }

        } else {
            if (orderSet.contains(orderNo)) {
                return new ResultInfo("正在处理，请稍后");
            }
            orderSet.add(orderNo);
            //还未结束
            String payerAppId = null;
            String payerUserAccount = null;
            StringBuffer payType2Str = new StringBuffer();
            String chargeStr = "";
            if (0 == 1 - orderInfo.getIsPaid()) {//订单状态已经改变了

            } else { //订单状态还未改变
                //是否需要查询第三方支付平台支付状态
                if (null != needQueryTHIStatus && !needQueryTHIStatus) {
                    //不需要查询（主要是paypal/stripe）
                    if (ValidateUtil.isNotNull(chargeId)) {
                        // stripe 传入了chargeId
                        chargeStr = ",chargeId = '" + chargeId + "'";
                    }
                } else {
                    //                    ResultInfo resultInfo = getTradeStatusQ(payType, payOrderNo, null);
                    ResultInfo resultInfo = null;
                    if (isPostmanJY) {
                        // 骑手的
                        resultInfo = getQsTradeStatusQ(payType, payOrderNo, null);
                    } else {
                        System.out.println("查询支付：" + orderNo + ",ChargeId:" + orderInfo.getChargeId() + ",payType:" + payType);
                        resultInfo = getTradeStatusQ(orderInfo.getAgentId(), appFlag, payType, userType, payOrderNo, orderInfo.getChargeId());
                    }
                    if (null == resultInfo) {
                        orderSet.remove(orderNo);
                        return new ResultInfo("请稍后重试");
                    } else {
                        if (!resultInfo.isSuccess()) {//失败，返回错误
                            System.out.println("订单支付成功后，查询订单失败1");
                            orderSet.remove(orderNo);
                            return resultInfo;
                        }
                        try {
                            QueryResult queryResult = (QueryResult) resultInfo.getObj();
                            if (null != queryResult) {
                                payerAppId = queryResult.getPayerId();
                                payerUserAccount = queryResult.getPayerAccount();

                                if (queryResult.isQuerySuccess() && queryResult.isPaySuccess() && ValidateUtil.isNotNull(orderInfo.getPayType())) {
                                    switch (orderInfo.getPayType()) {
                                        case ChannelUtils.yiyi: {
                                            String payType2 = queryResult.getNote();
                                            String paySign = orderInfo.getPaySign() + ";" + queryResult.getMsg();
                                            payType2Str.append(",payType2 = '").append(payType2).append("',paySign='").append(paySign).append("'");
                                        }
                                        break;
                                        case ChannelUtils.alipay:
                                        case ChannelUtils.wx:
                                        case ChannelUtils.wx_xcx: {
                                            if (null != SYS_SETTINGS.WX_ALI_USE_TRI && 0 == WX_ALI_TRI_CHANNEL_TYPE.NIHAOPAY - SYS_SETTINGS.WX_ALI_USE_TRI) {
                                                String chargeId2 = queryResult.getNote();
//                                                payType2Str.append(",chargeId = '").append(chargeId2).append("'");
                                                chargeStr = ",chargeId = '" + chargeId2 + "'";
                                            }
                                        }
                                        break;
                                    }
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }

                //如果是自取的话生成取货验证码
                String verifyCode = "";
                try {
                    if (0 == 1 - orderInfo.getIsZiQu()) {
                        verifyCode = ",verifyCode='" + OrderMan.getVerifyCodeDaodian() + "'";
                    } else if (SYS_SETTINGS.HAS_ORDER_SECOND_POSTMAN_VERIFY_CODE == 1 && (orderInfo.getShopSelfPost() != null && orderInfo.getShopSelfPost() == 0)) {
                        // 二级骑手验证码
                        verifyCode = ",verifyCode='" + OrderMan.getVerifyCode() + "'";
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (ValidateUtil.isNull(verifyCode)) {
                    verifyCode = "";
                }

                if (OrderMan.isMultipleParentOrder(orderInfo)) {
                    // 聚合訂單
                    ResultInfo resultInfo = modifyMultipleOrderStatus(orderNo, payType2Str);
                    if (resultInfo == null) {
                        System.out.println("聚合订单修改订单状态失败V1.1");
                        orderSet.remove(orderNo);
                        return new ResultInfo("更新订单状态失败，请稍后再试V1.1");
                    }
                    if (!resultInfo.isSuccess()) {
                        System.out.println(resultInfo.getError());
                        orderSet.remove(orderNo);
                        return new ResultInfo("更新订单状态失败，请稍后再试V1.1");
                    }
                } else {
                    // 获取该店铺的每日订单号
                    String orderPreNo = "";
                    if ((null == orderInfo.getPreDayNo() || 0 == orderInfo.getPreDayNo()) && null != orderInfo.getShopId() && null != orderInfo.getOrderType()) {
                        String shopOwn = "admin";
                        if (null != shopInfo) {
                            shopOwn = shopInfo.getOwnUsername();
                        }
                        int preDayNo = SubmitOrderMan.getShopPreDayNo(orderInfo.getShopId(), shopOwn, orderInfo.getUsername());
                        orderPreNo = ",preDayNo=" + preDayNo;
                    }

                    Long time = DateUtils.getCurrTime();
                    OrderStatus orderStatus = null;
                    int orderSta = ORDER_STATUS.PAYED;
                    if (orderInfo.getOrderType() != null && orderInfo.getIsArrivePay() != null && orderInfo.getIsArrivePay() == 1) {
                        orderSta = ORDER_STATUS.ARRIVE_PAY;
                        orderStatus = new OrderStatus(orderNo, orderSta, username, time);
                    } else if (orderInfo.getOrderType() != null && ORDER_TYPE.GROUP_ORDER == orderInfo.getOrderType()) {
                        orderStatus = new OrderStatus(orderNo, ORDER_STATUS.SHOP_SURE, username, time);
                        orderSta = ORDER_STATUS.SHOP_SURE;
                    } else if (orderInfo.getOrderType() != null && (orderInfo.getOrderType() == ORDER_TYPE.SHOP_SERVICE || orderInfo.getOrderType() == ORDER_TYPE.SHOP_RESCUE)) {
                        orderSta = ORDER_STATUS.SHOP_SELECT;
                        orderStatus = new OrderStatus(orderNo, ORDER_STATUS.SHOP_SELECT, username, time);
                    } else {
                        orderStatus = new OrderStatus(orderNo, ORDER_STATUS.PAYED, username, time);
                    }
                    //已成功支付，更新订单状态
                    StringBuilder sb = new StringBuilder();
//                    sb.append("update ").append(PojoTables.OrderInfo).append(" set isPaid =1,orderStatus=").append(ORDER_STATUS.PAYED).append(verifyCode).append(" where delFlag =0 and  orderNo = '").append(orderNo).append("'");
                    sb.append("update ").append(PojoTables.OrderInfo).append(" set ").append(orderSta - ORDER_STATUS.ARRIVE_PAY == 0 ? "" : " isPaid =1,")
                            .append(" orderStatus=").append(orderSta).append(verifyCode).append(orderPreNo)
                            .append(payType2Str.toString())
                            .append(chargeStr)
                            .append(" where delFlag =0 and  orderNo = '").append(orderNo).append("'");
                    try {
                        System.out.println("~~~~~" + sb.toString());
                        UserDaoSql.getInstance().executeBySql(sb.toString());
                        UserDaoSql.getInstance().insertObj(PojoTables.OrderStatus, orderStatus);
                    } catch (SQLException e) {
                        e.printStackTrace();
                        orderSet.remove(orderNo);
                        return new ResultInfo("更新订单状态失败，请稍后再试");
                    }
                    //更新缓存中订单状态
                    OrderInfoMemory.getInstance().paidModifyStatus(orderNo);
                    OrderStatusMemory.getInstance().addStatus(orderNo);
                }

                //跑腿订单
                if (null != orderInfo.getOrderType() && (
                        0 == ORDER_TYPE.PAO_TUI - orderInfo.getOrderType()
                                || 0 == ORDER_TYPE.DAI_GOU - orderInfo.getOrderType()
                                || 0 == ORDER_TYPE.BANG_BAN - orderInfo.getOrderType()
                                || 0 == ORDER_TYPE.PERSON_TRANSPORT - orderInfo.getOrderType()
                                || 0 == ORDER_TYPE.HUO_YUN - orderInfo.getOrderType())) { //跑腿订单
                    StringBuilder sbOther = new StringBuilder();
                    sbOther.append("update ").append(PojoTables.OTHER_ORDER_INFO).append(" set isPaid =1,orderStatus=").append(ORDER_STATUS.PAYED).append(verifyCode).append(" where delFlag =0 and  orderNo = '").append(orderNo).append("'");
                    try {
                        UserDaoSql.getInstance().executeBySql(sbOther.toString());
                    } catch (SQLException e) {
                        e.printStackTrace();
                        orderSet.remove(orderNo);
                        return new ResultInfo("更新订单状态失败，请稍后再试");
                    }
                    //缓存修改
                    OtherOrderMemory.getInstance().addOrModify(orderNo);
                    // 此处修改保险单的支付状态
                    if (null != orderInfo.getOrderType() && 0 == ORDER_TYPE.PERSON_TRANSPORT - orderInfo.getOrderType()) {
                        modifyOrderPolicyRecordPaidStatus(orderInfo, username);
                    }

                    // 订单模式 在抢单之前判断订单模式
                    if (SYS_SETTINGS.ADMIN_MANAGER_GRAB_ORDER_MODE == 0 && SYS_SETTINGS.GRAB_ORDER_MODE != null && GRAB_ORDER_MODE.ONLY_DISPATCH_MODE == SYS_SETTINGS.GRAB_ORDER_MODE) {
                        // 若是仅调度模式 不给骑手推送
                    } else {
                        // 推送给骑手
                        if (!OrderMan.isMultipleSonOrder(orderInfo)) {
                            JPushMessage jPushMessage = new JPushMessage(true, orderInfo.getShopCity(), "又有新的订单了", "又有新的订单了，快去抢单吧");
                            jPushMessage.setPushUserType(JPushMessage.POSTMAN);//推给配送员
                            jPushMessage.setJpSubType("");
                            JPushTask.jPushMessageList.add(jPushMessage);
                        }
                    }
                }


                // create by yangtk
                // if (orderInfo.getIsArrivePay() != null && 0 == 1 - orderInfo.getIsArrivePay()) {
                //     // 货到付款订单 付款不在进行推送
                // } else {
                if (OrderMan.isMultipleParentOrder(orderInfo)) {
                    // 聚合订单
                    multipleOrderNotice(sysAppKey, orderInfo);
                } else {
                    //订单支付成功 推送通知
                    String shopOwnUsername = "";
                    if (null != shopInfo) {
                        shopOwnUsername = shopInfo.getOwnUsername();
                        if (null != SYS_SETTINGS.SHOP_NEW_ORDER_SMS && 0 == 1 - SYS_SETTINGS.SHOP_NEW_ORDER_SMS) {
                            //发送短信
                            JYSmsSeverBusiness.sendSMS(sysAppKey, shopInfo.getTelephone(), SMS_TYPE.SHOP_NEW_ORDER);
                        }
                    }
                    if (ValidateUtil.isNull(sysAppKey)) {
                        sysAppKey = orderInfo.getSysAppKey();
                    }
                    if (orderInfo.getOrderType() != null && ORDER_TYPE.GROUP_ORDER == orderInfo.getOrderType()) {
                        // 团购商家不需要接收到通知
                        publishOrderNotice(sysAppKey, orderNo, username, null);
                    } else {
                        publishOrderNotice(sysAppKey, orderNo, username, shopOwnUsername);
                    }
                    //如果是等我5分钟那种商家不确定的  推送给所有商家
                    if (null == shopInfo && null != orderInfo.getOrderType() && 0 == ORDER_TYPE.SHOP_GOODS - orderInfo.getOrderType()) {
                        JPushMessage jPushMessage = new JPushMessage(true, orderInfo.getShopCity(), "又有新的订单了", "又有新的订单了，快去抢单吧");
                        jPushMessage.setPushUserType(JPushMessage.POSTMAN);//推给配送员
                        jPushMessage.setJpSubType("");
                        JPushTask.jPushMessageList.add(jPushMessage);
                    }
                    // }
                }
                //end
            }
            if (OrderMan.isMultipleParentOrder(orderInfo)) {
                ResultInfo resultInfo = OrderPayDistribute.addOrderPayMulti(orderInfo, shopInfo, payerAppId, payerUserAccount);
                if (null == resultInfo) {
                    orderSet.remove(orderNo);
                    return new ResultInfo("增加流水失败，请稍后再试。");
                } else {
                    if (!resultInfo.isSuccess()) {
                        System.out.println("订单支付成功后增加流水失败 m:" + orderNo);
                        orderSet.remove(orderNo);
                        return resultInfo;
                    }
                }
            } else {
                // 分成金额
                ResultInfo resultInfo = addOrderPay(orderNo, payType, payerAppId, payerUserAccount, username, orderInfo, shopInfo);// 订单回调业务处理
                orderSet.remove(orderNo);// 分成流水已处理，可以释放了
                if (null == resultInfo) {
                    return new ResultInfo("增加流水失败，请稍后再试。");
                } else {
                    if (!resultInfo.isSuccess()) {
                        System.out.println("订单支付成功后增加流水失败:" + orderNo);
                        return resultInfo;
                    }
                }
            }

            //开锁服务单给锁匠加积分
            if (null != orderInfo.getOrderType() && 0 == ORDER_TYPE.SERVICE_LOCK - orderInfo.getOrderType()) {//开锁的需要回访
                //完成订单
                //给锁匠推送已经支付成功的消息
                if (ValidateUtil.isNotNull(orderInfo.getPostManUsername())) {
                    //发送一条订单通知
                    Notice.publishNotice2Person(sysAppKey, orderInfo.getPostManUsername(), JPushMessage.POSTMAN, NoticeType.order, "工单信息", "您的订单（工单编号：" + orderNo + "）已支付成功");
                }
            }
        }

        //支付成功即完成订单
        if (null != SYS_SETTINGS.ORDER_PAID_IS_OVER && 0 == 1 - SYS_SETTINGS.ORDER_PAID_IS_OVER
                || (null != orderInfo.getOrderType() && 0 == ORDER_TYPE.SERVICE_LOCK - orderInfo.getOrderType())//开锁支付完就完了
                || (null != orderInfo.getOrderType() && 0 == ORDER_TYPE.WALLET_IN - orderInfo.getOrderType())//钱包支付完就玩了
                || (null != orderInfo.getOrderType() && 0 == ORDER_TYPE.OPEN_VIP_ORDER - orderInfo.getOrderType())//钱包支付完就玩了
                || (null != orderInfo.getOrderType() && 0 == ORDER_TYPE.WALLET_DEPOSIT_IN - orderInfo.getOrderType())//钱包支付完就玩了
        ) {
            orderInfo = OrderInfoMemory.getInstance().getOrderInfo(orderNo);
            if (null != orderInfo && 0 == ORDER_STATUS.PAYED - orderInfo.getOrderStatus()) {//支付成功后分成
                //分成
                ResultInfo resultInfo = OrderPay.distributeMoney(orderInfo, orderInfo.getOrderNo(), "admin");
                if (null != resultInfo && !resultInfo.isSuccess()) {
                    System.out.println("完成订单，但是分成失败：" + resultInfo.getError());
                }
            }
        } else {

            //加到线程，判断商家是否自动取消订单
            if (null != SYS_SETTINGS.ORDER_AUTO_CANCEL_TIME && SYS_SETTINGS.ORDER_AUTO_CANCEL_TIME > 0) {
                //外卖订单
                if (null != orderInfo.getOrderType() && (0 == ORDER_TYPE.SHOP_GOODS - orderInfo.getOrderType()
                        || 0 == ORDER_TYPE.PAO_TUI - orderInfo.getOrderType()
                        || 0 == ORDER_TYPE.DAI_GOU - orderInfo.getOrderType()
                        || 0 == ORDER_TYPE.PERSON_TRANSPORT - orderInfo.getOrderType()
                        || 0 == ORDER_TYPE.BANG_BAN - orderInfo.getOrderType()
                        || 0 == ORDER_TYPE.HUO_YUN - orderInfo.getOrderType())) {
                    CancelOrder4ShopThread.map.put(orderNo, orderInfo.getShopId());
                }
            }
        }
        /**
         * 分销处理-订单付款
         */
        if (null != SYS_SETTINGS.IS_OPEN_DISTRIBUTION && 0 == 1 - SYS_SETTINGS.IS_OPEN_DISTRIBUTION &&
                !OrderMan.isMultipleSonOrder(orderInfo)) { // 聚合订单子订单不发生分销
            String requrstStr = HttpRequest.sendPost(SYS_CONFIG_PATH.FEN_XIAO_SERVICE_URL + "/api/distribute/order/commision/pay"
                    , "username=" + orderInfo.getUsername() + "&orderNo=" + orderInfo.getOrderNo() + "&orderMoney=" +
                            orderInfo.getTotalPrice() + "&sysCustomer=" + SYS_CONFIG_PATH.FEN_XIAO_SERVICE_KEY + "&shopId=" + orderInfo.getShopId());
            DeviceCompanyContact companyContact = new Gson().fromJson(requrstStr, DeviceCompanyContact.class);
            if (null == companyContact) {
                System.out.println("分销失败 " + orderInfo.getOrderNo());
            }
            if (0 == 1 - companyContact.getStatus()) {
                System.out.println("分销处理-订单付款成功 " + orderInfo.getOrderNo());
            }
        }
        /**
         *  吃喝玩了商家优惠券红包功能
         */
        final Integer shopHasPayFinishPacket = SYS_SETTINGS.SHOP_HAS_PAY_FINISH_PACKET;


        // 订单未货到付款订单  不在推送接单
        // ceate by yangtk
        // if (orderInfo.getIsArrivePay() != null && 0 == 1 - orderInfo.getIsArrivePay()) {
        //     // 货到付款订单
        //     // 货到付款订单
        // } else {
        // 是否需要自动接单
        boolean needShopAppley = false;
        if (null != orderInfo.getOrderType() && 0 == ORDER_TYPE.SHOP_GOODS - orderInfo.getOrderType()) {
            //商城订单，是否为商家自动接单
            if (SYS_SETTINGS.SQUARE_ORDER_IS_AUTO_APPLY == 1) {
                System.out.println("进来自动判断是否进行接单了~~~");
                // 创业商圈订单自动接单~~~
                if (ValidateUtil.isID(orderInfo.getSquareShopId()) || OrderMan.isMultipleParentOrder(orderInfo) || OrderMan.isMultipleSonOrder(orderInfo)) {
                    System.out.println("进来自动判断是否进行接单了2~~~");
                    needShopAppley = true;
                }
                if (OrderMan.isMultipleParentOrder(orderInfo)) {
                    //
                    System.out.println("自动接单~~~~~->" + orderInfo.getOrderNo());
                    squareOrderAutoApplyOrder(sysAppKey, orderInfo.getOrderNo());
                }
                System.out.println("目前我这边是~~~~" + needShopAppley);
            } else if (null != SYS_SETTINGS.SHOP_AUTO_APPLY_BY_PLATFORM) {
                switch (SYS_SETTINGS.SHOP_AUTO_APPLY_BY_PLATFORM) {
                    case 0: {// 平台不为商家接单

                    }
                    break;
//                        case 1: {// 所有订单平台自动接单
//                            needShopAppley = true;
//                        }
//                        break;
                    case 1: {// 每个商家单独设置，是否为商家接单
                        if (null != shopInfo && null != shopInfo.getAutoApplyBySys() && 0 == 1 - shopInfo.getAutoApplyBySys()) {
                            needShopAppley = true;
                        }
                    }
                    break;
                    case 2: {
                        // 三种接单方式
                        // 强制自动接单
                        //	开启后：商家端设置不起作用，来了新单，平台（接口）为商家接单
                        //后台自动接单
                        //	开启后：如果商家端开启自动接单，那么平台（接口）为商家接单；如果商家端关闭自动接单，那么平台（接口）不为商家接单
                        //商户自由设置
                        //	平台（接口）不为商家做任何操作，由商家端自己处理
                        if (null != shopInfo && null != shopInfo.getAutoApplyBySys()) {
                            switch (shopInfo.getAutoApplyBySys()) {
                                case 0: {// 商家自己处理

                                }
                                break;
                                case 1: {// 强制接单
                                    //将订单置为商家已接单
                                    needShopAppley = true;
                                }
                                break;
                                case 2: {// 后台自动接单
                                    // 商家是否开启了自动接单,如果开启了，那么接口为商家接单
                                    if (0 == 1 - shopInfo.getAutoApply()) {
                                        needShopAppley = true;
                                    }
                                }
                                break;
                            }

                        }
                    }
                    break;
                }
            }
//                if (null != SYS_SETTINGS.SHOP_AUTO_APPLY_BY_PLATFORM && 0 == 1 - SYS_SETTINGS.SHOP_AUTO_APPLY_BY_PLATFORM) {
//                    //将订单置为商家已接单
//                    if (null != orderInfo.getOrderType() && 0 == ORDER_TYPE.SHOP_GOODS - orderInfo.getOrderType()) {
//                        if (null != shopInfo && 0 == 1 - shopInfo.getAutoApply()) {//店铺设置的是自动接单
//                            ShopOrderController.sureOrder(sysAppKey, shopInfo, orderInfo, "admin", true);
//                        }
//                    }
//                } else if (null != shopInfo && null != shopInfo.getAutoApplyBySys() && 0 == 1 - shopInfo.getAutoApplyBySys()) {
//                    //将订单置为商家已接单
//                    if (null != orderInfo.getOrderType() && 0 == ORDER_TYPE.SHOP_GOODS - orderInfo.getOrderType()) {
//                        ShopOrderController.sureOrder(sysAppKey, shopInfo, orderInfo, "admin", true);
//                    }
//                }
        }
        // 自动接单
        if (needShopAppley) {
            ShopOrderController.sureOrder(sysAppKey, shopInfo, orderInfo, "admin", true);
        }

        // }

        // 跑腿订单-支付后直接-自动派单
        if (null != orderInfo.getOrderType() && (
                0 == ORDER_TYPE.PAO_TUI - orderInfo.getOrderType()
                        || 0 == ORDER_TYPE.DAI_GOU - orderInfo.getOrderType()
                        || 0 == ORDER_TYPE.BANG_BAN - orderInfo.getOrderType()
                        || 0 == ORDER_TYPE.PERSON_TRANSPORT - orderInfo.getOrderType()
                        || 0 == ORDER_TYPE.HUO_YUN - orderInfo.getOrderType())) { //跑腿订单

            // 加入自动派单
            OrderTaskUtil.addNeedDispatchOrder(orderNo);
        }
        return new ResultInfo();
    }

    // 自动接创业商圈的订单
    private static void squareOrderAutoApplyOrder(String sysAppKey, String orderNo) {
        HashSet<String> orderSet = OrderInfoMemory.getInstance().getOrderNoByPayNo(orderNo);
        if (orderSet != null && orderSet.size() > 0) {
            for (String tNo : orderSet) {
                OrderInfo orderInfo = OrderInfoMemory.getInstance().getOrderInfo(tNo);
                if (orderInfo == null) {
                    continue;
                }
                ShopInfo shopInfo = null;
                if (OrderMan.isMultipleParentOrder(orderInfo)) {
                    shopInfo = ShopMemory.getInstance().getShopById(orderInfo.getSquareShopId());
                } else {
                    shopInfo = ShopMemory.getInstance().getShopById(orderInfo.getShopId());
                }
                if (shopInfo == null) {
                    continue;
                }
                ShopOrderController.sureOrder(sysAppKey, shopInfo, orderInfo, "admin", true);
            }
        }
    }

    // 修改本地的保险支付状态
    private static void modifyOrderPolicyRecordPaidStatus(OrderInfo orderInfo, String opUser) {
        if (SYS_SETTINGS.HAS_BAO_XIAN != null && SYS_SETTINGS.HAS_BAO_XIAN == 1 && orderInfo.getIsBuyInsurance() != null && orderInfo.getIsBuyInsurance() == 1) {
            String updateSql = "update " + PojoTables.ORDER_POLICY_RECORD + " set isOrderPaid = 1,updateUser ='" + opUser + "' where delFlag = 0 and orderNo ='" + orderInfo.getOrderNo() + "'";
            try {
                UserDaoSql.getInstance().executeBySql(updateSql);
            } catch (Exception e) {
                e.printStackTrace();
            }
            OrderPolicyRecordMemory.getInstance().addOrModify(orderInfo.getOrderNo());
        }

    }

    //查询订单支付状态
    private static ResultInfo getTradeStatusQ(Long agentId, String appFlag, String payType, Integer userType, String orderNo, String tradeNo) {
        if (ValidateUtil.isNull(payType)) {
            return new ResultInfo("请选择支付方式");
        }
        if (PAY_TYPE.ARRIVE.equalsIgnoreCase(payType) || PAY_TYPE.WALLET.equalsIgnoreCase(payType)) {
            return new ResultInfo();
        }
        // 货到付款
        if (PAY_TYPE.UNDERLINE.equalsIgnoreCase(payType) || PAY_TYPE.ARRIVE.equalsIgnoreCase(payType)) {
            return new ResultInfo();
        }
        QueryResult queryResult = PayUtils.getInstance().getTradeStatusQ(agentId, appFlag,
                payType, userType, orderNo, tradeNo);// getTradeStatusQ
        if (null == queryResult) {
            System.out.println("查询支付状态失败a");
            return new ResultInfo("查询支付状态失败，请稍后再试");
        }
        if (queryResult.isQuerySuccess()) {
            if (ValidateUtil.isNull(queryResult.getPayStatus())) {
                System.out.println("未获取到支付平台结果b");
                return new ResultInfo("未获取到支付平台结果，请稍后再试");
            }
            if (!TradeStatusUtils.TRADE_FINISHED.equals(queryResult.getPayStatus()) && !TradeStatusUtils.TRADE_SUCCESS.equals(queryResult.getPayStatus())) {//支付未成功
                System.out.println("未获取到支付平台结果c");
                return new ResultInfo("未获取到支付平台成功结果，请稍后再试");
            }

            //支付成功
            ResultInfo resultInfo = new ResultInfo();
            resultInfo.setObj(queryResult);
            //保存支付者id等
            savePayerId(orderNo, queryResult.getPayerId(), queryResult.getPayerAccount());

            return resultInfo;
        } else {
            System.out.println("查询支付失败d");
            return new ResultInfo(queryResult.getError());
        }
    }

    // 骑手微信 支付 查询订单支付状态
    private static ResultInfo getQsTradeStatusQ(String payType, String orderNo, String tradeNo) {
        if (ValidateUtil.isNull(payType)) {
            return new ResultInfo("请选择支付方式");
        }
        QueryResult queryResult = PayUtils.getInstance().getQSTradeStatusQ(payType, orderNo, tradeNo);
        if (null == queryResult) {
            System.out.println("查询支付状态失败a");
            return new ResultInfo("查询支付状态失败，请稍后再试");
        }
        if (queryResult.isQuerySuccess()) {
            if (ValidateUtil.isNull(queryResult.getPayStatus())) {
                System.out.println("未获取到支付平台结果b");
                return new ResultInfo("未获取到支付平台结果，请稍后再试");
            }
            if (!TradeStatusUtils.TRADE_FINISHED.equals(queryResult.getPayStatus()) && !TradeStatusUtils.TRADE_SUCCESS.equals(queryResult.getPayStatus())) {//支付未成功
                System.out.println("未获取到支付平台结果c");
                return new ResultInfo("未获取到支付平台成功结果，请稍后再试");
            }
            //支付成功
            ResultInfo resultInfo = new ResultInfo();
            resultInfo.setObj(queryResult);
            //保存支付者id等
            savePayerId(orderNo, queryResult.getPayerId(), queryResult.getPayerAccount());
            return resultInfo;
        } else {
            System.out.println("查询支付失败d");
            return new ResultInfo(queryResult.getError());
        }
    }


    //是否已支付成功
    public static boolean hasPaySuccess(String sysAppKey, String appFlag, String payType, String orderNo, String tradeNo) {
        return hasPaySuccess(sysAppKey, null, appFlag, payType, UserType.USER, orderNo, tradeNo);
    }


    public static boolean hasPaySuccess(String sysAppKey, Long agentId, String appFlag, String payType, Integer userType, String orderNo, String chargeId) {
        QueryResult queryResult = PayUtils.getInstance().getTradeStatusQ(agentId, appFlag, payType, userType, orderNo, chargeId);// hasPaySuccess
        if (null == queryResult) {
            return false;
        }
        if (queryResult.isQuerySuccess()) {
            if (ValidateUtil.isNull(queryResult.getPayStatus())) {
                return false;
            }
            if (!TradeStatusUtils.TRADE_FINISHED.equals(queryResult.getPayStatus()) && !TradeStatusUtils.TRADE_SUCCESS.equals(queryResult.getPayStatus())) {//支付未成功
                return false;
            }
            //支付成功
            if (!queryResult.isPaySuccess()) {
                return false;
            }
            //保存支付者id等
            savePayerId(orderNo, queryResult.getPayerId(), queryResult.getPayerAccount());
        } else {
            return false;
        }


        userOrderPayOver(sysAppKey, appFlag, payType, orderNo, chargeId);
        return true;
    }

    // 骑手端 是否已支付成功
    public static boolean qsHasPaySuccess(String sysAppKey, String payType, String orderNo, String chargeId) {
        return qsHasPaySuccess(sysAppKey, "", payType, orderNo, chargeId);
    }

    public static boolean qsHasPaySuccess(String sysAppKey, String appFlag, String payType, String orderNo, String chargeId) {
        QueryResult queryResult = PayUtils.getInstance().getQSTradeStatusQ(payType, orderNo, chargeId);
        if (null == queryResult) {
            return false;
        }
        if (queryResult.isQuerySuccess()) {
            if (ValidateUtil.isNull(queryResult.getPayStatus())) {
                return false;
            }
            if (!TradeStatusUtils.TRADE_FINISHED.equals(queryResult.getPayStatus()) && !TradeStatusUtils.TRADE_SUCCESS.equals(queryResult.getPayStatus())) {//支付未成功
                return false;
            }
            //支付成功
            //保存支付者id等
            savePayerId(orderNo, queryResult.getPayerId(), queryResult.getPayerAccount());
        } else {
            return false;
        }


        userOrderPayOver(sysAppKey, appFlag, payType, orderNo, chargeId);
        return true;
    }

//    //退款
//    public static boolean refund(String payType, String orderNo, String fcOrderNo, double money, String chargeId) {
//        if (ValidateUtil.isNull(payType) || ValidateUtil.isNull(orderNo)) {
//            return false;
//        }
//        if (ValidateUtil.isNotAbsDouble(money)) {
//            return false;
//        }
//        return PayUtils.getInstance().refund(payType, orderNo, fcOrderNo, money + "", chargeId);
//    }

    public static ResultInfo refundV2(Long agentId, String appFlag,
                                      String payType, String orderNo, String fcOrderNo, double money, Double originalRefundMoney, String chargeId) {
        if (ValidateUtil.isNull(payType) || ValidateUtil.isNull(orderNo)) {
//            return false;
            return new ResultInfo("订单号或支付类型为空");
        }
//        if (ValidateUtil.isNotAbsDouble(money)) {
////            return false;
//            return new ResultInfo("退款金额不正确");
//        }
        return PayUtils.getInstance().refundV2(agentId, appFlag,
                payType, orderNo, fcOrderNo, money + "", originalRefundMoney, chargeId);
    }

    //     部分退款
    public static boolean refundPart(Long agentId, String appFlag,
                                     String payType, String orderNo, String backOrderNo, double money, double refundMoney, String chargeId, boolean isUpdateRefund) {
        if (ValidateUtil.isNull(payType) || ValidateUtil.isNull(orderNo) || ValidateUtil.isNull(backOrderNo)) {
            return false;
        }
        if (ValidateUtil.isNotAbsDouble(money) || ValidateUtil.isNotAbsDouble(refundMoney)) {
            return false;
        }
        return PayUtils.getInstance().refundPart(agentId, appFlag, payType, orderNo, backOrderNo, money + "", refundMoney + "", chargeId, isUpdateRefund);
    }


    //增加用户钱包流水
    public static ResultInfo userAddFlow(OrderInfo orderInfo, String username) {
        if (null == orderInfo) {
            return new ResultInfo("订单信息有误");
        }
        if (ValidateUtil.isNotNull(username)) {
            //            OrderPayInfo orderPayInfo = OrderPayMemory.getInstance().get(orderInfo.getOrderNo());
            //            if (null == orderPayInfo) {
            //                return new ResultInfo("支付信息不存在");
            //            }
            UserWalletFlow userWalletFlow = new UserWalletFlow(username, UserType.USER, orderInfo.getOrderNo(), orderInfo.getOrderType(), orderInfo.getTotalPrice(),
                    orderInfo.getTotalPrice(), 1d, UserWalletFlowType.FLOW_COST,
                    username, DateUtils.getCurrTime());
            userWalletFlow.setAwardMoney(orderInfo.getAwardMoney());

            //汇率
            if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                userWalletFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                userWalletFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                userWalletFlow.setFcMoney(JYMathDoubleUtils.mul(orderInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                userWalletFlow.setFcAwardMoney(JYMathDoubleUtils.mul(orderInfo.getAwardMoney(), SYS_SETTINGS.EXCHANGE_RATE));
            }
            try {
                UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, userWalletFlow);//用户流水
            } catch (SQLException e) {
                e.printStackTrace();
                return new ResultInfo("创建余额支付流水失败.");
            }
        }


        return new ResultInfo();
    }


    //订单完成，分成
    synchronized public static ResultInfo distributeMoney(OrderInfo orderInfo, String orderNo, String opUsername) {
        if (ValidateUtil.isNull(orderNo)) {
            return new ResultInfo("订单信息有误");
        }

        // // 货到付款没有分成
        // if (null != orderInfo && null != orderInfo.getIsArrivePay() && orderInfo.getIsArrivePay() == 1) {
        //     return new ResultInfo();
        // }
        // 是否聚合订单
        if (OrderMan.isMultipleParentOrder(orderInfo)) {
            // 聚合订单分成
            return distributeMoneyMulti(orderInfo, orderNo, opUsername);
        }

        OrderPayInfo orderPayInfo = OrderPayMemory.getInstance().get(orderNo);
        if (null == orderPayInfo) {
            return new ResultInfo("订单信息有误.");
        }

        //是否已经添加过流水，添加过后不再添加
        if (UserWalletFlowMemory.getInstance().hasFlow(orderNo, UserWalletFlowType.FLOW_INCOME)) {
            return new ResultInfo();
        }

        if (null != orderInfo && null != orderInfo.getOrderType() && (
                0 == ORDER_TYPE.SHOP_GOODS - orderInfo.getOrderType()//外卖订单
                        || 0 == ORDER_TYPE.GROUP_ORDER - orderInfo.getOrderType()// 团购订单
                        || 0 == ORDER_TYPE.SHOP_ACTIVITY - orderInfo.getOrderType()//美羹 活动订单
                        || 0 == ORDER_TYPE.SHARE_GOODS_ORDER - orderInfo.getOrderType()//爱甩卖 分享商品购买订单
                        || 0 == ORDER_TYPE.PAO_TUI - orderInfo.getOrderType()
                        || 0 == ORDER_TYPE.DAI_GOU - orderInfo.getOrderType()
                        || 0 == ORDER_TYPE.BANG_BAN - orderInfo.getOrderType()
                        || 0 == ORDER_TYPE.PERSON_TRANSPORT - orderInfo.getOrderType()
                        || 0 == ORDER_TYPE.HUO_YUN - orderInfo.getOrderType())) {
            //0. 分享者
            if ((null != orderInfo.getOrderType() && (0 == ORDER_TYPE.GROUP_ORDER - orderInfo.getOrderType()))
                    && ValidateUtil.isNotNull(orderPayInfo.getShareUsername())) {
                Double sharerShareRate = orderPayInfo.getSharerShareRate();//分成比例
                Double shareMoney = orderPayInfo.getSharerShareMoney();//分成金额
                UserWalletFlow shopFlow = new UserWalletFlow(orderPayInfo.getShareUsername(), UserType.USER, orderPayInfo.getOrderNo(), orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                        sharerShareRate, shareMoney, UserWalletFlowType.FLOW_INCOME,
                        opUsername, DateUtils.getCurrTime());

                //汇率
                if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                    shopFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                    shopFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                    shopFlow.setFcMoney(JYMathDoubleUtils.mul(shareMoney, SYS_SETTINGS.EXCHANGE_RATE));
                }
                try {
                    UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, shopFlow);//分成 分享者分成比例
                } catch (SQLException e) {
                    e.printStackTrace();
                    return new ResultInfo("创建分享者分成流水失败.");
                }
                if (ValidateUtil.isAbsDouble(shareMoney)) {
                    // 分享者余额增加
                    ResultInfo resultInfo = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getShareUsername(), UserType.USER, null, UserWalletMan.ADD, shareMoney, 0d, opUsername);
                    if (null != resultInfo && !resultInfo.isSuccess()) {
                        System.out.println("更新分享者余额错误:" + resultInfo.getError());
                    }
                }
            }

            //1. 店铺
            if ((null != orderInfo.getOrderType() && (0 == ORDER_TYPE.SHOP_GOODS - orderInfo.getOrderType() || 0 == ORDER_TYPE.SHARE_GOODS_ORDER - orderInfo.getOrderType()))
                    && ValidateUtil.isNotNull(orderPayInfo.getSeller())) {
                Double shopRate = orderPayInfo.getShopRate();//分成比例
                Double shopMoney = orderPayInfo.getShopMoney();//分成金额
                UserWalletFlow shopFlow = new UserWalletFlow(orderPayInfo.getSeller(), UserType.SHOP_OWNER, orderPayInfo.getOrderNo(), orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                        shopMoney, shopRate, UserWalletFlowType.FLOW_INCOME,
                        opUsername, DateUtils.getCurrTime());
                // 需要账单审核?
                if (orderPayInfo.getSettlementState() == 0) {
                    shopFlow.setState(0);   // 表示入账中
                }
                //汇率
                if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                    shopFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                    shopFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                    shopFlow.setFcMoney(JYMathDoubleUtils.mul(shopMoney, SYS_SETTINGS.EXCHANGE_RATE));
                }

                try {
                    UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, shopFlow);//分成 店铺流水
                } catch (SQLException e) {
                    e.printStackTrace();
                    return new ResultInfo("创建店铺分配流水失败.");
                }
                if (shopFlow.getState() != 0) {
                    //店铺余额增加  v2
                    ResultInfo resultInfo = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getSeller(), UserType.SHOP_OWNER, orderPayInfo.getSellerId(), UserWalletMan.ADD, shopMoney, 0d, opUsername);
                    if (null != resultInfo && !resultInfo.isSuccess()) {
                        System.out.println("更新店铺余额错误:" + resultInfo.getError());
                    }
                }
            }

            //2. 配送员
            if (ValidateUtil.isNotNull(orderPayInfo.getPostman())) {
                Double postmanRate = orderPayInfo.getPostmanRate();
                Double postmanMoney = orderPayInfo.getPostmanMoney();
                UserWalletFlow postmanFlow = new UserWalletFlow(orderPayInfo.getPostman(), UserType.POSTMAN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                        postmanMoney, postmanRate, UserWalletFlowType.FLOW_INCOME,
                        opUsername, DateUtils.getCurrTime());

                //汇率
                if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                    postmanFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                    postmanFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                    postmanFlow.setFcMoney(JYMathDoubleUtils.mul(postmanMoney, SYS_SETTINGS.EXCHANGE_RATE));
                }

                try {
                    UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, postmanFlow);//分成 配送流水
                } catch (SQLException e) {
                    e.printStackTrace();
                    return new ResultInfo("创建配送员分配流水失败.");
                }

                if (ValidateUtil.isAbsDouble(postmanMoney)) {
                    //配送员余额增加
                    ResultInfo resultInfoPostMan = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getPostman(), UserType.POSTMAN, null, UserWalletMan.ADD, postmanMoney, 0d, opUsername);
                    if (null != resultInfoPostMan && !resultInfoPostMan.isSuccess()) {
                        System.out.println("更新配送员余额错误:" + resultInfoPostMan.getError());
                    }
                }
            }

            //2.2 二级配送员
            if (ValidateUtil.isNotNull(orderPayInfo.getSecondaryPostman())) {
                Double postmanRate = 0.0d;
                Double postmanMoney = orderPayInfo.getSecondaryPostmanMoney() == null ? 0.0d : orderPayInfo.getSecondaryPostmanMoney();
                UserWalletFlow postmanFlow = new UserWalletFlow(orderPayInfo.getSecondaryPostman(), UserType.POSTMAN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                        postmanMoney, postmanRate, UserWalletFlowType.FLOW_INCOME,
                        opUsername, DateUtils.getCurrTime());

                //汇率
                if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                    postmanFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                    postmanFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                    postmanFlow.setFcMoney(JYMathDoubleUtils.mul(postmanMoney, SYS_SETTINGS.EXCHANGE_RATE));
                }

                try {
                    UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, postmanFlow);//分成 配送流水
                } catch (SQLException e) {
                    e.printStackTrace();
                    return new ResultInfo("创建配送员分配流水失败.");
                }

                if (ValidateUtil.isAbsDouble(postmanMoney)) {
                    //配送员余额增加
                    ResultInfo resultInfoPostMan = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getSecondaryPostman(), UserType.POSTMAN, null, UserWalletMan.ADD, postmanMoney, 0d, opUsername);
                    if (null != resultInfoPostMan && !resultInfoPostMan.isSuccess()) {
                        System.out.println("更新配送员余额错误:" + resultInfoPostMan.getError());
                    }
                }
            }


            //3. 业务员 add by zhuwx 2019-2-12
            if (ValidateUtil.isNotNull(orderPayInfo.getBusiness())) {
                Double businessRate = orderPayInfo.getBusinessRate();
                Double businessMoney = orderPayInfo.getBusinessMoney();
                UserWalletFlow businessFlow = new UserWalletFlow(orderPayInfo.getBusiness(), UserType.BUSINESS_MAN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                        businessMoney, businessRate, UserWalletFlowType.FLOW_INCOME,
                        opUsername, DateUtils.getCurrTime());

                //汇率
                if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                    businessFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                    businessFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                    businessFlow.setFcMoney(JYMathDoubleUtils.mul(businessMoney, SYS_SETTINGS.EXCHANGE_RATE));
                }

                try {
                    UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, businessFlow);//分成 配送流水
                } catch (SQLException e) {
                    e.printStackTrace();
                    return new ResultInfo("创建业务员分配流水失败.");
                }
                if (ValidateUtil.isAbsDouble(businessMoney)) {
                    //业务员余额增加
                    ResultInfo businessResult = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getBusiness(), UserType.BUSINESS_MAN, null, UserWalletMan.ADD, businessMoney, 0d, opUsername);
                    if (null != businessResult && !businessResult.isSuccess()) {
                        System.out.println("更新业务员余额错误:" + businessResult.getError());
                    }
                }
            }
            //3.1 区域主管 add by yangtk 2019-12-12
            if (ValidateUtil.isNotNull(orderPayInfo.getAgentMangerUsername())) {
                Double agentMangerRate = orderPayInfo.getAgentManagerRate();
                Double agentMangerMoney = orderPayInfo.getAgentManagerMoney();
                System.out.println(orderPayInfo.getAgentMangerUsername());
                UserWalletFlow businessFlow = new UserWalletFlow(orderPayInfo.getAgentMangerUsername(), UserType.AGENT_MANAGER, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                        agentMangerMoney, agentMangerRate, UserWalletFlowType.FLOW_INCOME,
                        opUsername, DateUtils.getCurrTime());
                //汇率
                if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                    businessFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                    businessFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                    businessFlow.setFcMoney(JYMathDoubleUtils.mul(agentMangerMoney, SYS_SETTINGS.EXCHANGE_RATE));
                }

                try {
                    UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, businessFlow);//分成 配送流水
                } catch (SQLException e) {
                    e.printStackTrace();
                    return new ResultInfo("创建区域分管流水失败.");
                }

                if (ValidateUtil.isAbsDouble(agentMangerMoney)) {
                    //业务员余额增加
                    ResultInfo businessResult = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getAgentMangerUsername(), UserType.AGENT_MANAGER, null, UserWalletMan.ADD, agentMangerMoney, 0d, opUsername);
                    if (null != businessResult && !businessResult.isSuccess()) {
                        System.out.println("更新区域分管失败:" + businessResult.getError());
                    }
                }
            }

            //4. 城代 add by zhuwx 2019-2-12
            if (ValidateUtil.isNotNull(orderPayInfo.getCityAdmin())) {
                Double cityAdminRate = orderPayInfo.getCityAdminRate();
                Double cityAdminMoney = orderPayInfo.getCityAdminMoney();
                UserWalletFlow cityAdminFlow = new UserWalletFlow(orderPayInfo.getCityAdmin(), UserType.ADMIN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                        cityAdminMoney, cityAdminRate, UserWalletFlowType.FLOW_INCOME,
                        opUsername, DateUtils.getCurrTime());
                System.out.println("城代分成：" + orderPayInfo.getOrderNo() + "," + cityAdminMoney + ";订单状态：" + orderInfo.getOrderStatus());
                //汇率
                if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                    cityAdminFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                    cityAdminFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                    cityAdminFlow.setFcMoney(JYMathDoubleUtils.mul(cityAdminMoney, SYS_SETTINGS.EXCHANGE_RATE));
                }

                try {
                    UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, cityAdminFlow);//分成 配送流水
                } catch (SQLException e) {
                    e.printStackTrace();
                    return new ResultInfo("创建城代分配流水失败.");
                }
                if (null != cityAdminMoney && 0 != cityAdminMoney) {
                    // 城代余额增加
                    ResultInfo cityAdminResult = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getCityAdmin(), UserType.ADMIN, null, UserWalletMan.ADD, cityAdminMoney, 0d, opUsername);
                    if (null != cityAdminResult && !cityAdminResult.isSuccess()) {
                        System.out.println("更新城代余额错误:" + cityAdminResult.getError());
                    }
                }
            }
            // 直系代理
            if (ValidateUtil.isNotNull(orderPayInfo.getCityAdmin1())) {
                String cityAdmin = orderPayInfo.getCityAdmin1();
                Double cityAdminRate = orderPayInfo.getCityAdminRate1();
                Double cityAdminMoney = orderPayInfo.getCityAdminMoney1();
                if (cityAdminMoney != 0.0d && ValidateUtil.isAbsDouble(cityAdminRate)) {
                    UserWalletFlow cityAdminFlow = new UserWalletFlow(cityAdmin, UserType.ADMIN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                            cityAdminMoney, cityAdminRate, UserWalletFlowType.FLOW_INCOME,
                            opUsername, DateUtils.getCurrTime());

                    //汇率
                    if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                        cityAdminFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                        cityAdminFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                        cityAdminFlow.setFcMoney(JYMathDoubleUtils.mul(cityAdminMoney, SYS_SETTINGS.EXCHANGE_RATE));
                    }

                    try {
                        UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, cityAdminFlow);//分成 配送流水
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return new ResultInfo("创建城代分配流水失败.");
                    }
                    if (cityAdminMoney != null && cityAdminMoney != 0.0d) {
                        //业务员余额增加
                        ResultInfo cityAdminResult = UserWalletMan.saveOrUpdateUserWallet(cityAdmin, UserType.ADMIN, null, UserWalletMan.ADD, cityAdminMoney, 0d, opUsername);
                        if (null != cityAdminResult && !cityAdminResult.isSuccess()) {
                            System.out.println("更新城代余额错误:" + cityAdminResult.getError());
                        }
                    }
                }
            }
            // 二级代理分成信息
            if (ValidateUtil.isNotNull(orderPayInfo.getCityAdmin2())) {
                String cityAdmin = orderPayInfo.getCityAdmin2();
                Double cityAdminRate = orderPayInfo.getCityAdminRate2();
                Double cityAdminMoney = orderPayInfo.getCityAdminMoney2();
                if (cityAdminMoney != 0.0d && ValidateUtil.isAbsDouble(cityAdminRate)) {
                    UserWalletFlow cityAdminFlow = new UserWalletFlow(cityAdmin, UserType.ADMIN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                            cityAdminMoney, cityAdminRate, UserWalletFlowType.FLOW_INCOME,
                            opUsername, DateUtils.getCurrTime());

                    //汇率
                    if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                        cityAdminFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                        cityAdminFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                        cityAdminFlow.setFcMoney(JYMathDoubleUtils.mul(cityAdminMoney, SYS_SETTINGS.EXCHANGE_RATE));
                    }

                    try {
                        UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, cityAdminFlow);//分成 配送流水
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return new ResultInfo("创建城代分配流水失败.");
                    }
                    if (null != cityAdminMoney && cityAdminMoney != 0.0d) {
                        //业务员余额增加
                        ResultInfo cityAdminResult = UserWalletMan.saveOrUpdateUserWallet(cityAdmin, UserType.ADMIN, null, UserWalletMan.ADD, cityAdminMoney, 0d, opUsername);
                        if (null != cityAdminResult && !cityAdminResult.isSuccess()) {
                            System.out.println("更新城代余额错误:" + cityAdminResult.getError());
                        }
                    }
                }
            }

            // 三级代理
            if (ValidateUtil.isNotNull(orderPayInfo.getCityAdmin3())) {
                String cityAdmin = orderPayInfo.getCityAdmin3();
                Double cityAdminRate = orderPayInfo.getCityAdminRate3();
                Double cityAdminMoney = orderPayInfo.getCityAdminMoney3();
                if (cityAdminMoney != 0.0d && ValidateUtil.isAbsDouble(cityAdminRate)) {
                    UserWalletFlow cityAdminFlow = new UserWalletFlow(cityAdmin, UserType.ADMIN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                            cityAdminMoney, cityAdminRate, UserWalletFlowType.FLOW_INCOME,
                            opUsername, DateUtils.getCurrTime());

                    //汇率
                    if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                        cityAdminFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                        cityAdminFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                        cityAdminFlow.setFcMoney(JYMathDoubleUtils.mul(cityAdminMoney, SYS_SETTINGS.EXCHANGE_RATE));
                    }

                    try {
                        UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, cityAdminFlow);//分成 配送流水
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return new ResultInfo("创建城代分配流水失败.");
                    }
                    if (cityAdminMoney != null && cityAdminMoney != 0.0d) {
                        //业务员余额增加
                        ResultInfo cityAdminResult = UserWalletMan.saveOrUpdateUserWallet(cityAdmin, UserType.ADMIN, null, UserWalletMan.ADD, cityAdminMoney, 0d, opUsername);
                        if (null != cityAdminResult && !cityAdminResult.isSuccess()) {
                            System.out.println("更新城代余额错误:" + cityAdminResult.getError());
                        }
                    }
                }
            }
            // 四级代理分成
            if (ValidateUtil.isNotNull(orderPayInfo.getCityAdmin4())) {
                String cityAdmin = orderPayInfo.getCityAdmin4();
                Double cityAdminRate = orderPayInfo.getCityAdminRate4();
                Double cityAdminMoney = orderPayInfo.getCityAdminMoney4();
                if (cityAdminMoney != 0.0d && ValidateUtil.isAbsDouble(cityAdminRate)) {
                    UserWalletFlow cityAdminFlow = new UserWalletFlow(cityAdmin, UserType.ADMIN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                            cityAdminMoney, cityAdminRate, UserWalletFlowType.FLOW_INCOME,
                            opUsername, DateUtils.getCurrTime());

                    //汇率
                    if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                        cityAdminFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                        cityAdminFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                        cityAdminFlow.setFcMoney(JYMathDoubleUtils.mul(cityAdminMoney, SYS_SETTINGS.EXCHANGE_RATE));
                    }

                    try {
                        UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, cityAdminFlow);//分成 配送流水
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return new ResultInfo("创建城代分配流水失败.");
                    }
                    if (null != cityAdminMoney && cityAdminMoney != 0.0d) {
                        //业务员余额增加
                        ResultInfo cityAdminResult = UserWalletMan.saveOrUpdateUserWallet(cityAdmin, UserType.ADMIN, null, UserWalletMan.ADD, cityAdminMoney, 0d, opUsername);
                        if (null != cityAdminResult && !cityAdminResult.isSuccess()) {
                            System.out.println("更新城代余额错误:" + cityAdminResult.getError());
                        }
                    }
                }
            }

            //5. 平台
            Double platformRate = orderPayInfo.getPlatformRate();
            Double platformMoney = orderPayInfo.getPlatformMoney();
            UserWalletFlow systemFlow = new UserWalletFlow("admin", UserType.SUPER_ADMIN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                    platformMoney, platformRate, UserWalletFlowType.FLOW_INCOME,
                    opUsername, DateUtils.getCurrTime());
            //汇率
            if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                systemFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                systemFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                systemFlow.setFcMoney(JYMathDoubleUtils.mul(platformMoney, SYS_SETTINGS.EXCHANGE_RATE));
            }
            try {
                UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, systemFlow);//分成 平台流水
            } catch (SQLException e) {
                e.printStackTrace();
                return new ResultInfo("创建系统分配流水失败.");
            }
        } else if (null != orderInfo.getOrderType() && 0 == orderInfo.getOrderType() - ORDER_TYPE.WALLET_IN) { //支付成功 账户余额增加
            //钱包充值 直接给用户账号增加
            //增加流水
            UserWalletFlow walletFlow = new UserWalletFlow(orderPayInfo.getBuyer(), UserType.USER, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                    orderInfo.getTotalPrice(), 1d, UserWalletFlowType.FLOW_INCOME,
                    opUsername, DateUtils.getCurrTime());
            walletFlow.setAwardMoney(orderInfo.getAwardMoney());

            //汇率
            if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                walletFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                walletFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                walletFlow.setFcMoney(JYMathDoubleUtils.mul(orderInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                walletFlow.setFcAwardMoney(JYMathDoubleUtils.mul(orderInfo.getAwardMoney(), SYS_SETTINGS.EXCHANGE_RATE));
            }

            try {
                UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, walletFlow);//分成  用户充值流水
            } catch (SQLException e) {
                e.printStackTrace();
                return new ResultInfo("添加充值记录失败.");
            }

            //余额增加
            ResultInfo resultInfo = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getBuyer(), UserType.USER, null, UserWalletMan.ADD, orderInfo.getTotalPrice(), orderInfo.getAwardMoney(), opUsername);
            if (null != resultInfo && !resultInfo.isSuccess()) {
                System.out.println("用户充值，更新余额错误:" + resultInfo.getError());
            }
        } else if (null != orderInfo.getOrderType() && 0 == orderInfo.getOrderType() - ORDER_TYPE.WALLET_DEPOSIT_IN) {//支付成功 押金余额增加
            //钱包流水
            UserWalletFlow walletFlow = new UserWalletFlow(orderPayInfo.getBuyer(), UserType.SHOP_OWNER, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                    orderInfo.getTotalPrice(), 1d, UserWalletFlowType.FLOW_INCOME,
                    opUsername, DateUtils.getCurrTime());

            //汇率
            if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                walletFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                walletFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                walletFlow.setFcMoney(JYMathDoubleUtils.mul(orderInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
            }

            try {
                UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, walletFlow);//分成  押金流水
            } catch (SQLException e) {
                e.printStackTrace();
                return new ResultInfo("添加押金流水失败.");
            }

            //修改押金金额
            ResultInfo resultInfo = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getBuyer(), UserType.USER, null, UserWalletMan.DEPOSIT_IN, orderInfo.getTotalPrice(), 0d, opUsername);
            if (null != resultInfo && !resultInfo.isSuccess()) {
                System.out.println("缴纳押金，更新金额错误:" + resultInfo.getError());
            }
        } else if (null != orderInfo.getOrderType() && 0 == orderInfo.getOrderType() - ORDER_TYPE.SERVICE_LOCK) {//开锁服务单
            //获取分成
            if (null != orderInfo.getShopId()) {
                Long time = DateUtils.getCurrTime();
                //平台制单还是锁匠制单
                if (-3 == orderInfo.getShopId()) {//平台制单
                    //1. 执行者
                    if (ValidateUtil.isNotNull(orderPayInfo.getPostman())) {
                        Double postmanRate = orderPayInfo.getPostmanRate();
                        Double postmanMoney = orderPayInfo.getPostmanMoney();
                        UserWalletFlow postmanFlow = new UserWalletFlow(orderPayInfo.getPostman(), UserType.POSTMAN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                                postmanMoney, postmanRate, UserWalletFlowType.FLOW_INCOME,
                                opUsername, time);

                        //汇率
                        if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                            postmanFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                            postmanFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                            postmanFlow.setFcMoney(JYMathDoubleUtils.mul(postmanMoney, SYS_SETTINGS.EXCHANGE_RATE));
                        }

                        try {
                            UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, postmanFlow);//分成 配送流水
                        } catch (SQLException e) {
                            e.printStackTrace();
                            return new ResultInfo("创建配送员分配流水失败.");
                        }

                        if (ValidateUtil.isAbsDouble(postmanMoney)) {
                            //配送员余额增加
                            ResultInfo resultInfoPostMan = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getPostman(), UserType.POSTMAN, null, UserWalletMan.ADD, postmanMoney, 0d, opUsername);
                            if (null != resultInfoPostMan && !resultInfoPostMan.isSuccess()) {
                                System.out.println("更新配送员余额错误:" + resultInfoPostMan.getError());
                            }
                        }
                    }
                    //2. 平台
                    Double platformRate = orderPayInfo.getPlatformRate();
                    Double platformMoney = orderPayInfo.getPlatformMoney();
                    UserWalletFlow systemFlow = new UserWalletFlow("admin", UserType.SUPER_ADMIN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                            platformMoney, platformRate, UserWalletFlowType.FLOW_INCOME,
                            opUsername, DateUtils.getCurrTime());
                    systemFlow.setOrderType(orderInfo.getOrderType());

                    //汇率
                    if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                        systemFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                        systemFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                        systemFlow.setFcMoney(JYMathDoubleUtils.mul(platformMoney, SYS_SETTINGS.EXCHANGE_RATE));
                    }
                    try {
                        UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, systemFlow);//分成 平台流水
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return new ResultInfo("创建系统分配流水失败.");
                    }
                } else if (-4 == orderInfo.getShopId()) {//锁匠制单
                    //1. 制单人
                    String subUsername = orderInfo.getUsername();//制单人账号
                    if (ValidateUtil.isNotNull(subUsername)) {
                        Double shopRate = orderPayInfo.getShopRate();//分成比例
                        Double shopMoney = orderPayInfo.getShopMoney();//分成金额
                        UserWalletFlow shopFlow = new UserWalletFlow(subUsername, UserType.POSTMAN, orderPayInfo.getOrderNo(), orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                                shopMoney, shopRate, UserWalletFlowType.FLOW_INCOME,
                                opUsername, time);

                        //汇率
                        if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                            shopFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                            shopFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                            shopFlow.setFcMoney(JYMathDoubleUtils.mul(shopMoney, SYS_SETTINGS.EXCHANGE_RATE));
                        }
                        try {
                            UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, shopFlow);//分成 制单人流水
                        } catch (SQLException e) {
                            e.printStackTrace();
                            return new ResultInfo("创建店铺分配流水失败.");
                        }

                        //余额增加
                        ResultInfo resultInfo = UserWalletMan.saveOrUpdateUserWallet(subUsername, UserType.POSTMAN, null, UserWalletMan.ADD, shopMoney, 0d, opUsername);
                        if (null != resultInfo && !resultInfo.isSuccess()) {
                            System.out.println("更新店铺余额错误:" + resultInfo.getError());
                        }
                    }
                    //2. 执行者
                    if (ValidateUtil.isNotNull(orderPayInfo.getPostman())) {
                        Double postmanRate = orderPayInfo.getPostmanRate();
                        Double postmanMoney = orderPayInfo.getPostmanMoney();
                        UserWalletFlow postmanFlow = new UserWalletFlow(orderPayInfo.getPostman(), UserType.POSTMAN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                                postmanMoney, postmanRate, UserWalletFlowType.FLOW_INCOME,
                                opUsername, time);
                        //汇率
                        if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                            postmanFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                            postmanFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                            postmanFlow.setFcMoney(JYMathDoubleUtils.mul(postmanMoney, SYS_SETTINGS.EXCHANGE_RATE));
                        }

                        try {
                            UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, postmanFlow);//分成 配送流水
                        } catch (SQLException e) {
                            e.printStackTrace();
                            return new ResultInfo("创建配送员分配流水失败.");
                        }

                        if (ValidateUtil.isAbsDouble(postmanMoney)) {
                            //配送员余额增加
                            ResultInfo resultInfoPostMan = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getPostman(), UserType.POSTMAN, null, UserWalletMan.ADD, postmanMoney, 0d, opUsername);
                            if (null != resultInfoPostMan && !resultInfoPostMan.isSuccess()) {
                                System.out.println("更新配送员余额错误:" + resultInfoPostMan.getError());
                            }
                        }
                    }
                    //3. 平台
                    Double platformRate = orderPayInfo.getPlatformRate();
                    Double platformMoney = orderPayInfo.getPlatformMoney();
                    UserWalletFlow systemFlow = new UserWalletFlow("admin", UserType.SUPER_ADMIN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                            platformMoney, platformRate, UserWalletFlowType.FLOW_INCOME,
                            opUsername, DateUtils.getCurrTime());
                    systemFlow.setOrderType(orderInfo.getOrderType());
                    //汇率
                    if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                        systemFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                        systemFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                        systemFlow.setFcMoney(JYMathDoubleUtils.mul(platformMoney, SYS_SETTINGS.EXCHANGE_RATE));
                    }
                    try {
                        UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, systemFlow);//分成 平台流水
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return new ResultInfo("创建系统分配流水失败.");
                    }
                }
            }

            //更新锁匠积分等级


        }

        //缓存中新增
        UserWalletFlowMemory.getInstance().addFLow(orderNo);
        try {
            JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.USER_WALLET_FLOW_ADD).setStr01(orderNo);
            JmsSender.setTopicSender(notify);
        } catch (Exception e) {
        }

        return new ResultInfo();
    }

    // 聚合订单完成，分成（陈鲜生菜市场）
    synchronized public static ResultInfo distributeMoneyMulti(OrderInfo orderInfo, String orderNo, String
            opUsername) {
        if (ValidateUtil.isNull(orderNo)) {
            return new ResultInfo("订单信息有误");
        }
        // 是否聚合订单
        if (!OrderMan.isMultipleParentOrder(orderInfo)) {
            return new ResultInfo();
        }
        // 货到付款没有分成
        if (null != orderInfo && null != orderInfo.getIsArrivePay() && orderInfo.getIsArrivePay() == 1) {
            return new ResultInfo();
        }

        OrderPayInfo orderPayInfo = OrderPayMemory.getInstance().get(orderNo);
        if (null == orderPayInfo) {
            return new ResultInfo("订单信息有误2.");
        }
        // 主订单分成骑手和平台的
        //是否已经添加过流水，添加过后不再添加
        if (!UserWalletFlowMemory.getInstance().hasFlow(orderNo, UserWalletFlowType.FLOW_INCOME)) {
            if (null != orderInfo && null != orderInfo.getOrderType() && (
                    0 == ORDER_TYPE.SHOP_GOODS - orderInfo.getOrderType()//外卖订单
                            || 0 == ORDER_TYPE.SHOP_ACTIVITY - orderInfo.getOrderType()//美羹 活动订单
                            || 0 == ORDER_TYPE.SHARE_GOODS_ORDER - orderInfo.getOrderType()//爱甩卖 分享商品购买订单
                            || 0 == ORDER_TYPE.PAO_TUI - orderInfo.getOrderType()
                            || 0 == ORDER_TYPE.DAI_GOU - orderInfo.getOrderType()
                            || 0 == ORDER_TYPE.BANG_BAN - orderInfo.getOrderType()
                            || 0 == ORDER_TYPE.HUO_YUN - orderInfo.getOrderType())) {

                //2. 配送员
                if (ValidateUtil.isNotNull(orderPayInfo.getPostman())) {
                    Double postmanRate = orderPayInfo.getPostmanRate();
                    Double postmanMoney = orderPayInfo.getPostmanMoney();
                    UserWalletFlow postmanFlow = new UserWalletFlow(orderPayInfo.getPostman(), UserType.POSTMAN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                            postmanMoney, postmanRate, UserWalletFlowType.FLOW_INCOME,
                            opUsername, DateUtils.getCurrTime());

                    //汇率
                    if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                        postmanFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                        postmanFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                        postmanFlow.setFcMoney(JYMathDoubleUtils.mul(postmanMoney, SYS_SETTINGS.EXCHANGE_RATE));
                    }

                    try {
                        UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, postmanFlow);//分成 配送流水
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return new ResultInfo("创建配送员分配流水失败.");
                    }

                    if (ValidateUtil.isAbsDouble(postmanMoney)) {
                        //配送员余额增加
                        ResultInfo resultInfoPostMan = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getPostman(), UserType.POSTMAN, null, UserWalletMan.ADD, postmanMoney, 0d, opUsername);
                        if (null != resultInfoPostMan && !resultInfoPostMan.isSuccess()) {
                            System.out.println("更新配送员余额错误:" + resultInfoPostMan.getError());
                        }
                    }
                }

                //2.2 二级配送员
                if (ValidateUtil.isNotNull(orderPayInfo.getSecondaryPostman())) {
                    Double postmanRate = 0.0d;
                    Double postmanMoney = orderPayInfo.getSecondaryPostmanMoney() == null ? 0.0d : orderPayInfo.getSecondaryPostmanMoney();
                    UserWalletFlow postmanFlow = new UserWalletFlow(orderPayInfo.getSecondaryPostman(), UserType.POSTMAN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                            postmanMoney, postmanRate, UserWalletFlowType.FLOW_INCOME,
                            opUsername, DateUtils.getCurrTime());

                    //汇率
                    if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                        postmanFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                        postmanFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                        postmanFlow.setFcMoney(JYMathDoubleUtils.mul(postmanMoney, SYS_SETTINGS.EXCHANGE_RATE));
                    }

                    try {
                        UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, postmanFlow);//分成 配送流水
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return new ResultInfo("创建配送员分配流水失败.");
                    }

                    if (ValidateUtil.isAbsDouble(postmanMoney)) {
                        //配送员余额增加
                        ResultInfo resultInfoPostMan = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getSecondaryPostman(), UserType.POSTMAN, null, UserWalletMan.ADD, postmanMoney, 0d, opUsername);
                        if (null != resultInfoPostMan && !resultInfoPostMan.isSuccess()) {
                            System.out.println("更新配送员余额错误:" + resultInfoPostMan.getError());
                        }
                    }
                }


                //3. 业务员 add by zhuwx 2019-2-12
                if (ValidateUtil.isNotNull(orderPayInfo.getBusiness())) {
                    Double businessRate = orderPayInfo.getBusinessRate();
                    Double businessMoney = orderPayInfo.getBusinessMoney();
                    UserWalletFlow businessFlow = new UserWalletFlow(orderPayInfo.getBusiness(), UserType.BUSINESS_MAN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                            businessMoney, businessRate, UserWalletFlowType.FLOW_INCOME,
                            opUsername, DateUtils.getCurrTime());

                    //汇率
                    if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                        businessFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                        businessFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                        businessFlow.setFcMoney(JYMathDoubleUtils.mul(businessMoney, SYS_SETTINGS.EXCHANGE_RATE));
                    }

                    try {
                        UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, businessFlow);//分成 配送流水
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return new ResultInfo("创建业务员分配流水失败.");
                    }

                    if (ValidateUtil.isAbsDouble(businessMoney)) {
                        //业务员余额增加
                        ResultInfo businessResult = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getBusiness(), UserType.BUSINESS_MAN, null, UserWalletMan.ADD, businessMoney, 0d, opUsername);
                        if (null != businessResult && !businessResult.isSuccess()) {
                            System.out.println("更新业务员余额错误:" + businessResult.getError());
                        }
                    }
                }

                //4. 城代 add by zhuwx 2019-2-12
                if (ValidateUtil.isNotNull(orderPayInfo.getCityAdmin())) {
                    Double cityAdminRate = orderPayInfo.getCityAdminRate();
                    Double cityAdminMoney = orderPayInfo.getCityAdminMoney();
                    UserWalletFlow cityAdminFlow = new UserWalletFlow(orderPayInfo.getCityAdmin(), UserType.ADMIN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                            cityAdminMoney, cityAdminRate, UserWalletFlowType.FLOW_INCOME,
                            opUsername, DateUtils.getCurrTime());

                    //汇率
                    if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                        cityAdminFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                        cityAdminFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                        cityAdminFlow.setFcMoney(JYMathDoubleUtils.mul(cityAdminMoney, SYS_SETTINGS.EXCHANGE_RATE));
                    }

                    try {
                        UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, cityAdminFlow);//分成 配送流水
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return new ResultInfo("创建城代分配流水失败.");
                    }

                    if (ValidateUtil.isAbsDouble(cityAdminMoney)) {
                        //业务员余额增加
                        ResultInfo cityAdminResult = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getCityAdmin(), UserType.ADMIN, null, UserWalletMan.ADD, cityAdminMoney, 0d, opUsername);
                        if (null != cityAdminResult && !cityAdminResult.isSuccess()) {
                            System.out.println("更新城代余额错误:" + cityAdminResult.getError());
                        }
                    }
                }

                // 直系代理
                if (ValidateUtil.isNotNull(orderPayInfo.getCityAdmin1())) {
                    String cityAdmin = orderPayInfo.getCityAdmin1();
                    Double cityAdminRate = orderPayInfo.getCityAdminRate1();
                    Double cityAdminMoney = orderPayInfo.getCityAdminMoney1();
                    if (cityAdminMoney != 0.0d && ValidateUtil.isAbsDouble(cityAdminRate)) {
                        UserWalletFlow cityAdminFlow = new UserWalletFlow(cityAdmin, UserType.ADMIN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                                cityAdminMoney, cityAdminRate, UserWalletFlowType.FLOW_INCOME,
                                opUsername, DateUtils.getCurrTime());

                        //汇率
                        if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                            cityAdminFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                            cityAdminFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                            cityAdminFlow.setFcMoney(JYMathDoubleUtils.mul(cityAdminMoney, SYS_SETTINGS.EXCHANGE_RATE));
                        }

                        try {
                            UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, cityAdminFlow);//分成 配送流水
                        } catch (SQLException e) {
                            e.printStackTrace();
                            return new ResultInfo("创建城代分配流水失败.");
                        }
                        if (cityAdminMoney != null && cityAdminMoney != 0.0d) {
                            //业务员余额增加
                            ResultInfo cityAdminResult = UserWalletMan.saveOrUpdateUserWallet(cityAdmin, UserType.ADMIN, null, UserWalletMan.ADD, cityAdminMoney, 0d, opUsername);
                            if (null != cityAdminResult && !cityAdminResult.isSuccess()) {
                                System.out.println("更新城代余额错误:" + cityAdminResult.getError());
                            }
                        }


                    }
                }

                // 二级代理分成信息
                if (ValidateUtil.isNotNull(orderPayInfo.getCityAdmin2())) {
                    String cityAdmin = orderPayInfo.getCityAdmin2();
                    Double cityAdminRate = orderPayInfo.getCityAdminRate2();
                    Double cityAdminMoney = orderPayInfo.getCityAdminMoney2();
                    if (cityAdminMoney != 0.0d && ValidateUtil.isAbsDouble(cityAdminRate)) {
                        UserWalletFlow cityAdminFlow = new UserWalletFlow(cityAdmin, UserType.ADMIN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                                cityAdminMoney, cityAdminRate, UserWalletFlowType.FLOW_INCOME,
                                opUsername, DateUtils.getCurrTime());

                        //汇率
                        if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                            cityAdminFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                            cityAdminFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                            cityAdminFlow.setFcMoney(JYMathDoubleUtils.mul(cityAdminMoney, SYS_SETTINGS.EXCHANGE_RATE));
                        }

                        try {
                            UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, cityAdminFlow);//分成 配送流水
                        } catch (SQLException e) {
                            e.printStackTrace();
                            return new ResultInfo("创建城代分配流水失败.");
                        }
                        if (null != cityAdminMoney && cityAdminMoney != 0.0d) {
                            //业务员余额增加
                            ResultInfo cityAdminResult = UserWalletMan.saveOrUpdateUserWallet(cityAdmin, UserType.ADMIN, null, UserWalletMan.ADD, cityAdminMoney, 0d, opUsername);
                            if (null != cityAdminResult && !cityAdminResult.isSuccess()) {
                                System.out.println("更新城代余额错误:" + cityAdminResult.getError());
                            }
                        }
                    }
                }

                // 三级代理
                if (ValidateUtil.isNotNull(orderPayInfo.getCityAdmin3())) {
                    String cityAdmin = orderPayInfo.getCityAdmin3();
                    Double cityAdminRate = orderPayInfo.getCityAdminRate3();
                    Double cityAdminMoney = orderPayInfo.getCityAdminMoney3();
                    if (cityAdminMoney != 0.0d && ValidateUtil.isAbsDouble(cityAdminRate)) {
                        UserWalletFlow cityAdminFlow = new UserWalletFlow(cityAdmin, UserType.ADMIN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                                cityAdminMoney, cityAdminRate, UserWalletFlowType.FLOW_INCOME,
                                opUsername, DateUtils.getCurrTime());

                        //汇率
                        if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                            cityAdminFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                            cityAdminFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                            cityAdminFlow.setFcMoney(JYMathDoubleUtils.mul(cityAdminMoney, SYS_SETTINGS.EXCHANGE_RATE));
                        }

                        try {
                            UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, cityAdminFlow);//分成 配送流水
                        } catch (SQLException e) {
                            e.printStackTrace();
                            return new ResultInfo("创建城代分配流水失败.");
                        }
                        if (cityAdminMoney != null && cityAdminMoney != 0.0d) {
                            //业务员余额增加
                            ResultInfo cityAdminResult = UserWalletMan.saveOrUpdateUserWallet(cityAdmin, UserType.ADMIN, null, UserWalletMan.ADD, cityAdminMoney, 0d, opUsername);
                            if (null != cityAdminResult && !cityAdminResult.isSuccess()) {
                                System.out.println("更新城代余额错误:" + cityAdminResult.getError());
                            }
                        }
                    }
                }
                // 四级代理分成
                if (ValidateUtil.isNotNull(orderPayInfo.getCityAdmin4())) {
                    String cityAdmin = orderPayInfo.getCityAdmin4();
                    Double cityAdminRate = orderPayInfo.getCityAdminRate4();
                    Double cityAdminMoney = orderPayInfo.getCityAdminMoney4();
                    if (cityAdminMoney != 0.0d && ValidateUtil.isAbsDouble(cityAdminRate)) {
                        UserWalletFlow cityAdminFlow = new UserWalletFlow(cityAdmin, UserType.ADMIN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                                cityAdminMoney, cityAdminRate, UserWalletFlowType.FLOW_INCOME,
                                opUsername, DateUtils.getCurrTime());

                        //汇率
                        if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                            cityAdminFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                            cityAdminFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                            cityAdminFlow.setFcMoney(JYMathDoubleUtils.mul(cityAdminMoney, SYS_SETTINGS.EXCHANGE_RATE));
                        }

                        try {
                            UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, cityAdminFlow);//分成 配送流水
                        } catch (SQLException e) {
                            e.printStackTrace();
                            return new ResultInfo("创建城代分配流水失败.");
                        }
                        if (null != cityAdminMoney && cityAdminMoney != 0.0d) {
                            //业务员余额增加
                            ResultInfo cityAdminResult = UserWalletMan.saveOrUpdateUserWallet(cityAdmin, UserType.ADMIN, null, UserWalletMan.ADD, cityAdminMoney, 0d, opUsername);
                            if (null != cityAdminResult && !cityAdminResult.isSuccess()) {
                                System.out.println("更新城代余额错误:" + cityAdminResult.getError());
                            }
                        }
                    }
                }

                //5. 平台
                Double platformRate = orderPayInfo.getPlatformRate();
                Double platformMoney = orderPayInfo.getPlatformMoney();
                UserWalletFlow systemFlow = new UserWalletFlow("admin", UserType.SUPER_ADMIN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                        platformMoney, platformRate, UserWalletFlowType.FLOW_INCOME,
                        opUsername, DateUtils.getCurrTime());
                //汇率
                if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                    systemFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                    systemFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                    systemFlow.setFcMoney(JYMathDoubleUtils.mul(platformMoney, SYS_SETTINGS.EXCHANGE_RATE));
                }
                try {
                    UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, systemFlow);//分成 平台流水
                } catch (SQLException e) {
                    e.printStackTrace();
                    return new ResultInfo("创建系统分配流水失败.");
                }
            }
            //缓存中新增
            UserWalletFlowMemory.getInstance().addFLow(orderNo);
            try {
                JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.USER_WALLET_FLOW_ADD).setStr01(orderNo);
                JmsSender.setTopicSender(notify);
            } catch (Exception e) {
            }
        }


        // 子订单 分成店铺的
        HashSet<String> childSet = OrderInfoMemory.getInstance().listOfParentOrderNo(orderNo);
        if (null == childSet || childSet.size() < 1) {
            return new ResultInfo();
        }
        for (String childOrderNo : childSet) {
            if (ValidateUtil.isNull(childOrderNo)) {
                continue;
            }

            //是否已经添加过流水，添加过后不再添加
            if (UserWalletFlowMemory.getInstance().hasFlow(childOrderNo, UserWalletFlowType.FLOW_INCOME)) {
                continue;
            }

            OrderPayInfo orderPay = OrderPayMemory.getInstance().get(childOrderNo);
            if (null == orderPay) {
                continue;
            }
            OrderInfo childOrder = OrderInfoMemory.getInstance().getOrderInfo(childOrderNo);
            if (null == childOrder) {
                continue;
            }

            // 分成
            if (null != childOrder && null != childOrder.getOrderType() && (
                    0 == ORDER_TYPE.SHOP_GOODS - childOrder.getOrderType()//外卖订单
                            || 0 == ORDER_TYPE.SHOP_ACTIVITY - childOrder.getOrderType()//美羹 活动订单
                            || 0 == ORDER_TYPE.SHARE_GOODS_ORDER - childOrder.getOrderType()//爱甩卖 分享商品购买订单
                            || 0 == ORDER_TYPE.PAO_TUI - childOrder.getOrderType()
                            || 0 == ORDER_TYPE.DAI_GOU - childOrder.getOrderType()
                            || 0 == ORDER_TYPE.BANG_BAN - childOrder.getOrderType()
                            || 0 == ORDER_TYPE.HUO_YUN - childOrder.getOrderType())) {
                //1. 店铺
                if ((null != childOrder.getOrderType() && (0 == ORDER_TYPE.SHOP_GOODS - childOrder.getOrderType() || 0 == ORDER_TYPE.SHARE_GOODS_ORDER - childOrder.getOrderType()))
                        && ValidateUtil.isNotNull(orderPayInfo.getSeller())) {
                    Double shopRate = orderPayInfo.getShopRate();//分成比例
                    Double shopMoney = orderPayInfo.getShopMoney();//分成金额
                    UserWalletFlow shopFlow = new UserWalletFlow(orderPayInfo.getSeller(), UserType.SHOP_OWNER, childOrderNo, childOrder.getOrderType(), orderPayInfo.getTotalPrice(),
                            shopMoney, shopRate, UserWalletFlowType.FLOW_INCOME,
                            opUsername, DateUtils.getCurrTime());

                    //汇率
                    if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                        shopFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                        shopFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                        shopFlow.setFcMoney(JYMathDoubleUtils.mul(shopMoney, SYS_SETTINGS.EXCHANGE_RATE));
                    }

                    try {
                        UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, shopFlow);//分成 店铺流水
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return new ResultInfo("创建店铺分配流水失败.");
                    }

                    //店铺余额增加  v2
                    ResultInfo resultInfo = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getSeller(), UserType.SHOP_OWNER, orderPayInfo.getSellerId(), UserWalletMan.ADD, shopMoney, 0d, opUsername);
                    if (null != resultInfo && !resultInfo.isSuccess()) {
                        System.out.println("更新店铺余额错误:" + resultInfo.getError());
                    }
                }

                //缓存中新增
                UserWalletFlowMemory.getInstance().addFLow(childOrderNo);
                try {
                    JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.USER_WALLET_FLOW_ADD).setStr01(childOrderNo);
                    JmsSender.setTopicSender(notify);
                } catch (Exception e) {
                }
            }
        }
        return new ResultInfo();

    }

    // 二级骑手确认完成  添加分成信息
    synchronized public static ResultInfo updateDistributeSecondaryMoney(OrderInfo orderInfo, String
            orderNo, String secondaryPostman) {
        if (ValidateUtil.isNull(orderNo)) {
            return new ResultInfo("订单信息有误");
        }
        if (StringUtils.isEmpty(secondaryPostman)) {
            return new ResultInfo("二级骑手账号不能为空");
        }
        OrderPayInfo orderPayInfo = OrderPayMemory.getInstance().get(orderNo);
        if (null == orderPayInfo) {
            return new ResultInfo("订单信息有误.");
        }
        CityAgent cityAgent = CityAgentMemory.getInstance().getById(orderInfo.getAgentId());
        Double secondaryMoney = 0.0d;
        Long agentId = 0L;
        if (cityAgent != null && ValidateUtil.isAbsDouble(cityAgent.getSecondaryPsMoney())) {
            secondaryMoney = cityAgent.getSecondaryPsMoney();
        }
        Double plateMoney = orderPayInfo.getPlatformMoney() == null ? 0.0d : orderPayInfo.getPlatformMoney();

        if (ValidateUtil.isID(orderInfo.getAgentId())) {
            agentId = orderInfo.getAgentId();
        }
        if (ValidateUtil.isNotAbsDouble(secondaryMoney)) {
            System.out.println("二级骑手工资未设置agentId:" + agentId);
            return new ResultInfo();
        }
        //
        plateMoney = JYMathDoubleUtils.sub(plateMoney, secondaryMoney);
        StringBuilder sql = new StringBuilder();
        sql.append(" update ").append(PojoTables.OrderPay).append(" set updateUser ='").append(secondaryPostman).append("'")
                .append(",secondaryPostman='").append(secondaryPostman).append("',platformMoney=").append(plateMoney)
                .append(",secondaryPostmanMoney=").append(secondaryMoney).append(" where orderNo =").append(orderNo).append(" and delFlag = 0");
        try {
            UserDaoSql.getInstance().executeBySql(sql.toString());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("二级骑手分成失败");
            return new ResultInfo("修改二级骑手分成失败");
        }
        // 缓存
        OrderPayMemory.getInstance().modify(orderNo);
        // 成功
        return new ResultInfo();
    }

    //取消订单，退还分成
    public static ResultInfo refundDistributeMoney(String orderNo, String opUsername, Integer orderType,
                                                   boolean isJudgeDuty) {
        if (ValidateUtil.isNull(orderNo)) {
            return new ResultInfo("订单信息有误");
        }

        OrderPayInfo orderPayInfo = OrderPayMemory.getInstance().get(orderNo);
        if (null == orderPayInfo) {
            return new ResultInfo("订单信息有误.");
        }

        //是否已经退过一次了
        if (0 == 1 - orderPayInfo.getIsOrderCancel()) {
            return new ResultInfo();
        }
        OrderInfo orderInfo = OrderInfoMemory.getInstance().getOrderInfo(orderNo);
        if (orderInfo == null) {
            return new ResultInfo("未获取到订单信息");
        }
        // 已完成的订单退款由谁来承担? 1 商家
        if (isJudgeDuty
                && orderInfo.getOrderType() != null && (orderInfo.getOrderType() - ORDER_TYPE.SHOP_GOODS == 0 || orderInfo.getOrderType() - ORDER_TYPE.GROUP_ORDER == 0)
                && SYS_SETTINGS.ORDER_FINISH_BACK_DUTY != null && SYS_SETTINGS.ORDER_FINISH_BACK_DUTY == 1) {
            Double shopRate = 0.0d; // 分成比例在此处没有意义
            Double shopMoney = orderPayInfo.getTotalPrice();        // 店铺来承担所有的金额
            UserWalletFlow shopFlow = new UserWalletFlow(orderPayInfo.getSeller(), UserType.SHOP_OWNER, orderPayInfo.getOrderNo(), orderType, orderPayInfo.getTotalPrice(),
                    shopMoney, shopRate, UserWalletFlowType.FLOW_COST,
                    opUsername, DateUtils.getCurrTime());

            //汇率
            if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                shopFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                shopFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                shopFlow.setFcMoney(JYMathDoubleUtils.mul(shopMoney, SYS_SETTINGS.EXCHANGE_RATE));
            }
            try {
                UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, shopFlow);//退款 店铺流水
            } catch (SQLException e) {
                e.printStackTrace();
                return new ResultInfo("创建店铺分配流水失败.");
            }

            //店铺余额减少  v2
            ResultInfo resultInfo = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getSeller(), UserType.SHOP_OWNER, orderPayInfo.getSellerId(), UserWalletMan.REFUND, shopMoney, 0d, opUsername);
            if (null != resultInfo && !resultInfo.isSuccess()) {
                System.out.println("更新店铺余额错误:" + resultInfo.getError());
            }
            return resultInfo;
        }

        //1. 店铺
        if (ValidateUtil.isNotNull(orderPayInfo.getSeller())) {
            Double shopRate = orderPayInfo.getShopRate();//分成比例
            Double shopMoney = orderPayInfo.getShopMoney();//分成金额
            UserWalletFlow shopFlow = new UserWalletFlow(orderPayInfo.getSeller(), UserType.SHOP_OWNER, orderPayInfo.getOrderNo(), orderType, orderPayInfo.getTotalPrice(),
                    shopMoney, shopRate, UserWalletFlowType.FLOW_COST,
                    opUsername, DateUtils.getCurrTime());

            //汇率
            if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                shopFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                shopFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                shopFlow.setFcMoney(JYMathDoubleUtils.mul(shopMoney, SYS_SETTINGS.EXCHANGE_RATE));
            }
            try {
                UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, shopFlow);//退款 店铺流水
            } catch (SQLException e) {
                e.printStackTrace();
                return new ResultInfo("创建店铺分配流水失败.");
            }

            //店铺余额减少  v2
            ResultInfo resultInfo = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getSeller(), UserType.SHOP_OWNER, orderPayInfo.getSellerId(), UserWalletMan.REFUND, shopMoney, 0d, opUsername);
            if (null != resultInfo && !resultInfo.isSuccess()) {
                System.out.println("更新店铺余额错误:" + resultInfo.getError());
            }
        }

        //2. 配送员
        if (ValidateUtil.isNotNull(orderPayInfo.getPostman())) {
            Double postmanRate = orderPayInfo.getPostmanRate();
            Double postmanMoney = orderPayInfo.getPostmanMoney();
            UserWalletFlow postmanFlow = new UserWalletFlow(orderPayInfo.getPostman(), UserType.POSTMAN, orderNo, orderType, orderPayInfo.getTotalPrice(),
                    postmanMoney, postmanRate, UserWalletFlowType.FLOW_COST,
                    opUsername, DateUtils.getCurrTime());
            //汇率
            if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                postmanFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                postmanFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                postmanFlow.setFcMoney(JYMathDoubleUtils.mul(postmanMoney, SYS_SETTINGS.EXCHANGE_RATE));
            }
            try {
                UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, postmanFlow);//退款 配送流水
            } catch (SQLException e) {
                e.printStackTrace();
                return new ResultInfo("创建配送员分配流水失败.");
            }

            if (ValidateUtil.isAbsDouble(postmanMoney)) {
                //配送员余额减少
                ResultInfo resultInfoPostMan = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getPostman(), UserType.POSTMAN, null, UserWalletMan.REFUND, postmanMoney, 0d, opUsername);
                if (null != resultInfoPostMan && !resultInfoPostMan.isSuccess()) {
                    System.out.println("更新配送员余额错误:" + resultInfoPostMan.getError());
                }
            }
        }


        //3. 平台
        Double platformRate = orderPayInfo.getPlatformRate();
        Double platformMoney = orderPayInfo.getPlatformMoney();
        UserWalletFlow systemFlow = new UserWalletFlow("admin", UserType.SUPER_ADMIN, orderNo, orderType, orderPayInfo.getTotalPrice(),
                platformMoney, platformRate, UserWalletFlowType.FLOW_COST,
                opUsername, DateUtils.getCurrTime());
        //汇率
        if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
            systemFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
            systemFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
            systemFlow.setFcMoney(JYMathDoubleUtils.mul(platformMoney, SYS_SETTINGS.EXCHANGE_RATE));
        }
        try {
            UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, systemFlow);//退款 平台流水
        } catch (SQLException e) {
            e.printStackTrace();
            return new ResultInfo("创建系统分配流水失败.");
        }


        // 直系代理
        if (ValidateUtil.isNotNull(orderPayInfo.getCityAdmin())) {
            Double adminRate = orderPayInfo.getCityAdminRate();
            Double adminMoney = orderPayInfo.getCityAdminMoney();
            UserWalletFlow adminFlow = new UserWalletFlow(orderPayInfo.getCityAdmin(), UserType.ADMIN, orderNo, orderType, orderPayInfo.getTotalPrice(),
                    adminMoney, adminRate, UserWalletFlowType.FLOW_COST,
                    opUsername, DateUtils.getCurrTime());
            //汇率
            if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                adminFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                adminFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                adminFlow.setFcMoney(JYMathDoubleUtils.mul(adminMoney, SYS_SETTINGS.EXCHANGE_RATE));
            }
            try {
                UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, adminFlow);//退款 流水
            } catch (SQLException e) {
                e.printStackTrace();
                return new ResultInfo("创建配送员分配流水失败.");
            }

            if (ValidateUtil.isAbsDouble(adminMoney)) {
                // 代理
                ResultInfo resultInfo = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getCityAdmin(), UserType.ADMIN, null, UserWalletMan.REFUND, adminMoney, 0d, opUsername);
                if (null != resultInfo && !resultInfo.isSuccess()) {
                    System.out.println("更新代理余额错误:" + resultInfo.getError());
                }
            }
        }
        // 一级代理
        if (ValidateUtil.isNotNull(orderPayInfo.getCityAdmin1())) {
            Double adminRate = orderPayInfo.getCityAdminRate1();
            Double adminMoney = orderPayInfo.getCityAdminMoney1();
            UserWalletFlow adminFlow = new UserWalletFlow(orderPayInfo.getCityAdmin1(), UserType.ADMIN, orderNo, orderType, orderPayInfo.getTotalPrice(),
                    adminMoney, adminRate, UserWalletFlowType.FLOW_COST,
                    opUsername, DateUtils.getCurrTime());
            //汇率
            if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                adminFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                adminFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                adminFlow.setFcMoney(JYMathDoubleUtils.mul(adminMoney, SYS_SETTINGS.EXCHANGE_RATE));
            }
            try {
                UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, adminFlow);//退款 流水
            } catch (SQLException e) {
                e.printStackTrace();
                return new ResultInfo("创建配送员分配流水失败.");
            }

            if (ValidateUtil.isAbsDouble(adminMoney)) {
                // 代理
                ResultInfo resultInfo = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getCityAdmin1(), UserType.ADMIN, null, UserWalletMan.REFUND, adminMoney, 0d, opUsername);
                if (null != resultInfo && !resultInfo.isSuccess()) {
                    System.out.println("更新代理余额错误:" + resultInfo.getError());
                }
            }
        }
        // 二级代理
        if (ValidateUtil.isNotNull(orderPayInfo.getCityAdmin2())) {
            Double adminRate = orderPayInfo.getCityAdminRate2();
            Double adminMoney = orderPayInfo.getCityAdminMoney2();
            UserWalletFlow adminFlow = new UserWalletFlow(orderPayInfo.getCityAdmin2(), UserType.ADMIN, orderNo, orderType, orderPayInfo.getTotalPrice(),
                    adminMoney, adminRate, UserWalletFlowType.FLOW_COST,
                    opUsername, DateUtils.getCurrTime());
            //汇率
            if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                adminFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                adminFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                adminFlow.setFcMoney(JYMathDoubleUtils.mul(adminMoney, SYS_SETTINGS.EXCHANGE_RATE));
            }
            try {
                UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, adminFlow);//退款 流水
            } catch (SQLException e) {
                e.printStackTrace();
                return new ResultInfo("创建配送员分配流水失败.");
            }

            if (ValidateUtil.isAbsDouble(adminMoney)) {
                // 代理
                ResultInfo resultInfo = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getCityAdmin2(), UserType.ADMIN, null, UserWalletMan.REFUND, adminMoney, 0d, opUsername);
                if (null != resultInfo && !resultInfo.isSuccess()) {
                    System.out.println("更新代理余额错误:" + resultInfo.getError());
                }
            }
        }
        // 三级代理
        if (ValidateUtil.isNotNull(orderPayInfo.getCityAdmin3())) {
            Double adminRate = orderPayInfo.getCityAdminRate3();
            Double adminMoney = orderPayInfo.getCityAdminMoney3();
            UserWalletFlow adminFlow = new UserWalletFlow(orderPayInfo.getCityAdmin3(), UserType.ADMIN, orderNo, orderType, orderPayInfo.getTotalPrice(),
                    adminMoney, adminRate, UserWalletFlowType.FLOW_COST,
                    opUsername, DateUtils.getCurrTime());
            //汇率
            if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                adminFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                adminFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                adminFlow.setFcMoney(JYMathDoubleUtils.mul(adminMoney, SYS_SETTINGS.EXCHANGE_RATE));
            }
            try {
                UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, adminFlow);//退款 流水
            } catch (SQLException e) {
                e.printStackTrace();
                return new ResultInfo("创建配送员分配流水失败.");
            }

            if (ValidateUtil.isAbsDouble(adminMoney)) {
                // 代理
                ResultInfo resultInfo = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getCityAdmin3(), UserType.ADMIN, null, UserWalletMan.REFUND, adminMoney, 0d, opUsername);
                if (null != resultInfo && !resultInfo.isSuccess()) {
                    System.out.println("更新代理余额错误:" + resultInfo.getError());
                }
            }
        }
        // 四级代理
        if (ValidateUtil.isNotNull(orderPayInfo.getCityAdmin4())) {
            Double adminRate = orderPayInfo.getCityAdminRate4();
            Double adminMoney = orderPayInfo.getCityAdminMoney4();
            UserWalletFlow adminFlow = new UserWalletFlow(orderPayInfo.getCityAdmin3(), UserType.ADMIN, orderNo, orderType, orderPayInfo.getTotalPrice(),
                    adminMoney, adminRate, UserWalletFlowType.FLOW_COST,
                    opUsername, DateUtils.getCurrTime());
            //汇率
            if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                adminFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                adminFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                adminFlow.setFcMoney(JYMathDoubleUtils.mul(adminMoney, SYS_SETTINGS.EXCHANGE_RATE));
            }
            try {
                UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, adminFlow);//退款 流水
            } catch (SQLException e) {
                e.printStackTrace();
                return new ResultInfo("创建配送员分配流水失败.");
            }

            if (ValidateUtil.isAbsDouble(adminMoney)) {
                // 代理
                ResultInfo resultInfo = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getCityAdmin4(), UserType.ADMIN, null, UserWalletMan.REFUND, adminMoney, 0d, opUsername);
                if (null != resultInfo && !resultInfo.isSuccess()) {
                    System.out.println("更新代理余额错误:" + resultInfo.getError());
                }
            }
        }

        return new ResultInfo();
    }


    //保存退款编号
    public static void saveRefundOrderCode(String orderNo, String fcOrderNo, String refundNo, String payType) {
        if (ValidateUtil.isNull(orderNo) || ValidateUtil.isNull(refundNo)) {
            return;
        }
        if (ValidateUtil.isNotNull(fcOrderNo)) {
            orderNo = fcOrderNo;
        }
        OrderInfo orderInfo = OrderInfoMemory.getInstance().getOrderInfo(orderNo);
        if (null == orderInfo) {
            return;
        }
        StringBuffer sb = new StringBuffer();
        sb.append("update ").append(PojoTables.OrderInfo).append(" set refundNo = '").append(refundNo).append("' where orderNo = '").append(orderNo).append("' and delFlag = 0");
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return;
        }

        OrderInfoMemory.getInstance().modifyStatus(orderNo);
    }

    // 部分退款保存退款编号
    public static boolean savePartRefundOrderCode(String orderNo, String refundNo, String payType) {
        if (ValidateUtil.isNull(orderNo) || ValidateUtil.isNull(refundNo)) {
            return false;
        }
        OrderCancelInfo orderInfo = OrderCancelInfoMemory.getInstance().get(orderNo);
        if (null == orderInfo) {
            return false;
        }
        StringBuffer sb = new StringBuffer();
        sb.append("update ").append(PojoTables.ORDER_CANCEL_INFO).append(" set refundNo = '").append(refundNo).append("' where orderNo = '").append(orderNo).append("' and delFlag = 0");
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

        OrderCancelInfoMemory.getInstance().modify(orderNo);
        return true;
    }


    //----------------------------
    private static void savePayerId(String orderNo, String payerAppId, String payerUserAccount) {
        if (ValidateUtil.isNull(orderNo) || ValidateUtil.isNull(payerAppId)) {
            return;
        }
        StringBuffer sb = new StringBuffer();
        sb.append("update ").append(PojoTables.OrderPay).append(" set buyerAppId='").append(payerAppId).append("',buyerLogonId='")
                .append(payerUserAccount).append("' where orderNo = '").append(orderNo).append("' and delFlag = 0");
        try {
            System.out.println(sb.toString());
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return;
        }
    }

    //发送极光推送
    private static void publishOrderNotice(String sysAppKey, String orderNo, String username, String
            shopOwnUsername) {
        if (ValidateUtil.isNotNull(username)) {
            //发送一条订单通知
            //            Notice.publishOrderNotice(username, "订单信息", "您的订单（订单编号：" + orderNo + "）已支付成功");

            //极光推送
            //            JPushMessage jPushMessage2 = new JPushMessage(username, "订单信息", "您的订单（订单编号：" + orderNo + "）已支付成功");
            //            jPushMessage2.setPushUserType(JPushMessage.USER);//推给用户
            //            JPushTask.jPushMessageList.add(jPushMessage2);
            Notice.publishNotice2Person(sysAppKey, username, JPushMessage.USER, NoticeType.order, "订单信息", "您的订单（订单编号：" + orderNo + "）已支付成功");
        }

        if (ValidateUtil.isNotNull(shopOwnUsername)) {
            //极光推送
            //            JPushMessage jPushMessage = new JPushMessage(shopOwnUsername, "又有新的订单了", "有新的订单，请尽快处理");
            //            jPushMessage.setPushUserType(JPushMessage.SHOP_OWNER);//发给店主
            //            JPushTask.jPushMessageList.add(jPushMessage);

            Notice.publishNotice2Person(sysAppKey, shopOwnUsername, JPushMessage.SHOP_OWNER, NoticeType.order, "又有新的订单了", "有新的订单，请尽快处理");
        }
    }


    //订单收入情况（分成比例）
    public static ResultInfo addOrderPay(String orderNo, String payType, String payerAppId, String
            payerUserAccount, String username, OrderInfo orderInfo, ShopInfo shopInfo) {
        if (null == orderInfo) {
            return new ResultInfo("分成出错，订单信息为空·");
        }

        ResultInfo resultInfo = new ResultInfo();
        if (null != orderInfo.getOrderType()) {
            switch (orderInfo.getOrderType()) {
                case ORDER_TYPE.SERVICE_LOCK: { //开锁服务单
                    resultInfo = addServiceLockOrderPay(orderNo, payType, payerAppId, payerUserAccount, username, orderInfo, shopInfo);
                    if (null == resultInfo) {
                        return new ResultInfo("新增订单支付信息失败");
                    }
                    //更新锁匠积分及等级
                    updatePostmanLevel(orderNo, orderInfo.getTotalPrice(), orderInfo.getPostManUsername(), username, orderInfo.getOrderType());
                }
                break;
                // 好管家(维修和救援订单)
                case ORDER_TYPE.SHOP_SERVICE:
                case ORDER_TYPE.SHOP_RESCUE: {
                    resultInfo = addRepairOrderPay(orderNo, payType, payerAppId, payerUserAccount, username, orderInfo, shopInfo);
                    if (null == resultInfo) {
                        return new ResultInfo("新增订单支付信息失败");
                    }
                }
                break;
                default: {
                    if (shopInfo == null) {
                        shopInfo = ShopMemory.getInstance().getShopById(orderInfo.getShopId());
                    }
                    if (SYS_SETTINGS.CUSTOM_SYSTEM_TYPE == 1) {
                    } else {
                        resultInfo = OrderPayDistribute.addNormalOrderPayV2(orderNo, payType, payerAppId, payerUserAccount, username, orderInfo, shopInfo);
                    }
                    if (null == resultInfo) {
                        return new ResultInfo("新增订单支付信息失败");
                    }
                }
                break;
            }
        }

        return resultInfo;
    }


    // 维修和购物订单分成
    // 维修订单 分成
    private static ResultInfo addRepairOrderPay(String orderNo, String payType, String payerAppId, String
            payerUserAccount, String username, OrderInfo orderInfo, ShopInfo shopInfo) {
        if (null == orderInfo) {
            return new ResultInfo("分成出错，订单信息为空·");
        }
        //add by zhuwx 2018-9-25
        OrderPayInfo orderPayInfoR = OrderPayMemory.getInstance().get(orderNo);
        if (null != orderPayInfoR) {
            return new ResultInfo("已经分成过了");
        }

        // 订单总金额
        Double totalGoodsMoney = JYMathDoubleUtils.add(orderInfo.getTotalGoodsMoney() != null ? orderInfo.getTotalGoodsMoney() : 0.0d, orderInfo.getOtherMoney() != null ? orderInfo.getOtherMoney() : 0.0d);
        totalGoodsMoney = JYMathDoubleUtils.add(totalGoodsMoney, orderInfo.getAppointPayMoney() != null ? orderInfo.getAppointPayMoney() : 0.0d);
        //支付金额
        Double payMoney = orderInfo.getTotalPrice();//用户支付的金额

        Double shopRate = 0d;
        Double shopTotalMoney = totalGoodsMoney;//店铺实际订单金额
        Double shopMoney = 0d;//店铺实际收入 分成后
        Double postmanRate = 0d;
        Double postmanTotalMoney = 0d;
        Double postmanMoney = 0d;

        //活动金额处理
        Double shopAwardMoney = orderInfo.getShopAwardMoney();//活动满减金额-店铺减金额
        Double platformAwardMoney = orderInfo.getPlatformAwardMoney();//活动满减金额-平台减金额

        Long sellerId = orderInfo.getShopId();
        String sellerUsername = "";

        //1.店铺分成
        if (null != shopInfo) {
            sellerId = shopInfo.getId();
            sellerUsername = shopInfo.getOwnUsername();
            shopRate = shopInfo.getMoneyRate();//分配比例
            postmanRate = shopInfo.getWorkerRate(); // 分成比例
        }

        if (null != shopInfo && null != shopInfo.getIsSelfPost() && 0 == 1 - shopInfo.getIsSelfPost()) {
            //店铺是自配送
            Integer isDeliveryShare = SYS_SETTINGS.SHOP_SHARE_DELIVERY_MONEY;//店铺分成时  订单的配送费是否参与分成 1 参与
            if (0 == 1 - isDeliveryShare) { //店铺分成时  配送费参与分成
                //配送费店铺参与分成
                //                    shopTotalMoney = JYMathDoubleUtils.add(totalGoodsMoney, deliveryPrice);//订单总金额减配送费
            }
        }
        //如果店铺有奖励金
        if (shopAwardMoney > 0) {
            shopTotalMoney = JYMathDoubleUtils.sub(shopTotalMoney, shopAwardMoney);//订单所有的钱 减去店铺奖励的金额
        }

        if (ValidateUtil.isAbsDouble(shopRate)) { //单独设置了店铺分成比例
        } else { //没有单独设置  按平台比例
            if (null == SYS_SETTINGS.SHOP_RATE_PEISONG) {
                System.out.println("未设置配送类店铺分成比例");
                return new ResultInfo("操作失败，请稍后再试。3");
            }
            shopRate = Double.parseDouble(SYS_SETTINGS.SHOP_RATE_PEISONG + "");
        }

        if (ValidateUtil.isAbsDouble(postmanRate)) { //单独设置了店铺分成比例
        } else { //没有单独设置  按平台比例
            if (null == SYS_SETTINGS.SHOP_WORKER_RATE) {
                System.out.println("未设置工人分成比例");
                return new ResultInfo("操作失败，请稍后再试。4");
            }
            postmanRate = Double.parseDouble(SYS_SETTINGS.SHOP_WORKER_RATE + "");
        }
        shopMoney = JYMathDoubleUtils.mul(shopTotalMoney, JYMathDoubleUtils.div(shopRate, 100));//分配金额  此时orderTotalMoney是订单所有的钱 减掉不应该有的配送费 再减掉店铺奖励的金额了
        //        System.out.println("shoprate1-->"+shopRate);
        // 2.工人部分
        if (ValidateUtil.isNull(orderInfo.getPostManUsername())) {
            // 没有工人 店铺拿走工人分成
            shopRate = JYMathDoubleUtils.add(shopRate, postmanRate);
            //            System.out.println("shoprate2-->"+shopRate);
            shopMoney = JYMathDoubleUtils.mul(shopTotalMoney, JYMathDoubleUtils.div(shopRate, 100));//分配金额
            postmanRate = 0.0d;
        } else {
            postmanMoney = JYMathDoubleUtils.mul(shopTotalMoney, JYMathDoubleUtils.div(postmanRate, 100));//分配金额
        }

        //3. 平台分成金额
        Double platformRate = JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(100, shopRate), postmanRate); //平台分成比例
        //平台收入 = 支付金额-分成给店铺-分成给配送员
        //        Double platformMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(payMoney, shopTotalMoney), postmanMoney);
        //        if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 == SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {//打包费全给商家
        //            platformMoney = JYMathDoubleUtils.sub(platformMoney, packetPrice);//给商家了 所以减掉打包费
        //        }
        Double platformMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(payMoney, shopMoney), postmanMoney);  // 平台收入 = 支付金额-分成给店铺-分成给配送员

        OrderPayInfo orderPayInfo = new OrderPayInfo(orderNo, orderInfo.getTotalPrice(), orderInfo.getUsername(), sellerUsername, sellerId, payType, 1, username, 0, DateUtils.getCurrTime(),
                JYMathDoubleUtils.div(shopRate, 100), shopMoney, JYMathDoubleUtils.div(postmanRate, 100), postmanMoney, JYMathDoubleUtils.div(platformRate, 100), platformMoney,
                shopAwardMoney, platformAwardMoney);
        orderPayInfo.setBuyerAppId(payerAppId);
        orderPayInfo.setBuyerLogonId(payerUserAccount);


        //外币记录
        if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY) { //是否使用主币支付 1使用主币支付 0使用外币支付
            //外币支付的
            if (ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {
                //外币汇率
                orderPayInfo.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
            }
        }

        try {
            UserDaoSql.getInstance().insertObj(PojoTables.OrderPay, orderPayInfo);
        } catch (SQLException e) {
            e.printStackTrace();
            return new ResultInfo("创建流水失败");
        }
        //缓存流水
        OrderPayMemory.getInstance().add(orderNo);// 维修订单
        try {
            JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.ORDER_PAY_ADD).setStr01(orderNo);
            JmsSender.setTopicSender(notify);
        } catch (Exception e) {
        }
        return new ResultInfo();
    }

    //开锁订单分成收入
    //订单收入算法
    //三方收入
    //1. 制单人
    //2. 执行人
    //3. 转单制单人
    private static ResultInfo addServiceLockOrderPay(String orderNo, String payType, String payerAppId, String
            payerUserAccount, String username, OrderInfo orderInfo, ShopInfo shopInfo) {
        if (null == orderInfo) {
            return new ResultInfo("分成出错，订单信息为空·");
        }

        //支付金额
        Double payMoney = orderInfo.getTotalPrice();//用户支付的金额

        //执行人
        Double postmanRate = 0d;
        Double postmanTotalMoney = 0d;
        Double postmanMoney = 0d;

        //转发单 制单人
        Double shopRate = 0d;
        Double shopTotalMoney = 0d;//店铺实际订单金额 未分成前（商品总金额 加上应该加的配送费 减掉店铺减的金额）
        Double shopMoney = 0d;//店铺实际收入 分成后

        //平台
        Double platformRate = 0d;
        Double platformMoney = 0d;//

        //开锁费用
        Double openMoney = 0d;
        //换锁费用
        Double changeMoney = 0d;
        List<OrderItems> itemList = OrderItemsMemory.getInstance().listOfOrderNo(orderNo);
        if (ValidateUtil.isAbsList(itemList)) {
            //没有费用
            for (int i = 0; i < itemList.size(); i++) {
                if (null == itemList.get(i)) {
                    continue;
                }
                if (ValidateUtil.isNotAbsDouble(itemList.get(i).getTotalMoney())) {
                    continue;
                }
                if (ValidateUtil.isNull(itemList.get(i).getName())) {
                    openMoney += itemList.get(i).getTotalMoney();
                } else {
                    if ("服务费".equalsIgnoreCase(itemList.get(i).getName())) {//开锁费用
                        openMoney += itemList.get(i).getTotalMoney();
                    } else {//换锁费用
                        changeMoney += itemList.get(i).getTotalMoney();
                    }
                }
            }
        }


        StringBuffer extra3Sb = new StringBuffer();
        extra3Sb.append("{");

        //工人等级
        String postManUsername = orderInfo.getPostManUsername();
        CityPostman postman = CityPostmanMemory.getInstance().getPostman(postManUsername);
        if (null == postman) {
            return new ResultInfo("锁匠不存在，添加订单支付信息失败");
        }
        Integer level = postman.getLevel();
        //平台制单，还是锁匠制单后转到平台
        if (-3 == orderInfo.getShopId()) {//-3平台发单
            //获取分成信息
            List<SysVariableDistribute> vList = SysVariableDistributeMemory.getInstance().listOfType(orderInfo.getOrderType(), level, 1);//平台制单
            //type2  平台5、执行者4
            //type3  开锁 1、换锁 2

            if (ValidateUtil.isAbsList(vList)) {
                Double platRate = 0d; //平台手续费

                //开锁分成情况
                Double openExeRate = 0d; //开锁执行者分成
                Double openExeMoney = 0d; //开锁执行者分成
                Double openPlatRate = 0d;//开锁平台分成
                //            Double openPlatMoney = 0d;//开锁平台分成

                //换锁执行者分成
                Double changeExeRate = 0d; //换锁执行者分成
                Double changeExeMoney = 0d; //换锁执行者分成
                Double changePlatRate = 0d;//换锁平台分成
                //            Double changePlatMoney = 0d;//换锁平台分成


                //开锁  type3=1
                //执行人分成 type2=4
                //平台分成   type2=5

                //换锁 type3=2
                //执行人分成 type2=4
                //平台分成   type2=5
                for (int i = 0; i < vList.size(); i++) {
                    SysVariableDistribute vD = vList.get(i);
                    if (null == vD) {
                        continue;
                    }

                    if (null == vD.getType2() || null == vD.getType3()) {
                        continue;
                    }

                    if (0 == vD.getType2()) {
                        platRate = vD.getVal();//平台手续费
                        platRate = JYMathDoubleUtils.div(platRate, 100);
                    }

                    switch (vD.getType2()) {//角色
                        case 4: {//type2 执行者4
                            switch (vD.getType3()) {//费用类型
                                case 1: { //type3  开锁 1
                                    openExeRate = vD.getVal();
                                    openExeRate = JYMathDoubleUtils.div(openExeRate, 100);
                                }
                                break;
                                case 2: { //type3  换锁 2
                                    changeExeRate = vD.getVal();
                                    changeExeRate = JYMathDoubleUtils.div(changeExeRate, 100);
                                }
                                break;
                            }
                        }
                        break;
                        case 5: {//type2  平台5
                            switch (vD.getType3()) {//费用类型
                                case 1: { //type3  开锁 1
                                    openPlatRate = vD.getVal();
                                    openPlatRate = JYMathDoubleUtils.div(openPlatRate, 100);
                                }
                                break;
                                case 2: { //type3  换锁 2
                                    changePlatRate = vD.getVal();
                                    changePlatRate = JYMathDoubleUtils.div(changePlatRate, 100);
                                }
                                break;
                            }
                        }
                        break;
                    }
                }

                //执行者
                //开锁执行者分成
                if (null != openExeRate) {
                    openExeMoney = JYMathDoubleUtils.mul(openMoney, openExeRate);
                    extra3Sb.append("\"openExeRate\":").append(openExeRate).append(",");
                    extra3Sb.append("\"openExeMoney\":").append(openExeMoney).append(",");
                }
                //换锁执行者分成
                if (null != changeExeRate) {
                    changeExeMoney = JYMathDoubleUtils.mul(changeMoney, changeExeRate);
                    extra3Sb.append("\"changeExeRate\":").append(changeExeRate).append(",");
                    extra3Sb.append("\"changeExeMoney\":").append(changeExeMoney).append(",");
                }
                extra3Sb.append("\"openMoney\":").append(openMoney).append(",");
                extra3Sb.append("\"changeMoney\":").append(changeMoney).append(",");
                //平台比例
                extra3Sb.append("\"platRate\":").append(platRate).append(",");
                extra3Sb.append("\"openPlatRate\":").append(openPlatRate).append(",");
                extra3Sb.append("\"changePlatRate\":").append(changePlatRate).append(",");

                //执行者最终金额及比例
                postmanMoney = JYMathDoubleUtils.add(openExeMoney, changeExeMoney);//执行者金额
                postmanRate = JYMathDoubleUtils.div(postmanMoney, payMoney);//执行者综合比例

                //平台比例
                platformRate = JYMathDoubleUtils.sub(1, postmanRate);//平台分成比例
                //平台收入 = 支付金额-分成给执行者的金额
                platformMoney = JYMathDoubleUtils.sub(payMoney, postmanMoney);


                //插入流水
                OrderPayInfo orderPayInfo = new OrderPayInfo(orderNo, orderInfo.getTotalPrice(), orderInfo.getUsername(), null, -3l, payType, 1, username, 0, DateUtils.getCurrTime(),
                        shopRate, shopMoney, postmanRate, postmanMoney, platformRate, platformMoney,
                        0d, 0d);
                orderPayInfo.setBuyerAppId(payerAppId);
                orderPayInfo.setBuyerLogonId(payerUserAccount);
                extra3Sb.append("\"level\":").append(level);
                extra3Sb.append("}");
                orderPayInfo.setExtra3(extra3Sb.toString());
                orderPayInfo.setPostman(orderInfo.getPostManUsername());
                try {
                    UserDaoSql.getInstance().insertObj(PojoTables.OrderPay, orderPayInfo);
                } catch (SQLException e) {
                    e.printStackTrace();
                    return new ResultInfo("创建流水失败1");
                }
            }
        } else if (-4 == orderInfo.getShopId()) {//-4工人发单
            //获取分成信息
            List<SysVariableDistribute> vList = SysVariableDistributeMemory.getInstance().listOfType(orderInfo.getOrderType(), level, 2);//锁匠制单

            //type2  平台5、制单人41、执行者4
            //type3  开锁 1、换锁 2

            if (ValidateUtil.isAbsList(vList)) {
                Double platRate = 0d; //平台手续费

                //开锁分成情况
                Double openExeRate = 0d; //开锁执行者分成
                Double openExeMoney = 0d; //开锁执行者分成
                Double openPlatRate = 0d;//开锁平台分成
                //            Double openPlatMoney = 0d;//开锁平台分成
                Double openSubmitRate = 0d; //开锁首发者分成
                Double openSubmitMoney = 0d; //开锁首发者分成

                //换锁执行者分成
                Double changeExeRate = 0d; //换锁执行者分成
                Double changeExeMoney = 0d; //换锁执行者分成
                Double changePlatRate = 0d;//换锁平台分成
                //            Double changePlatMoney = 0d;//换锁平台分成
                Double changeSubmitRate = 0d; //换锁首发者分成
                Double changeSubmitMoney = 0d; //换锁首发者分成

                //开锁  type3=1
                //执行人分成 type2=4
                //平台分成   type2=5

                //换锁 type3=2
                //执行人分成 type2=4
                //平台分成   type2=5
                for (int i = 0; i < vList.size(); i++) {
                    SysVariableDistribute vD = vList.get(i);
                    if (null == vD) {
                        continue;
                    }

                    if (null == vD.getType2() || null == vD.getType3()) {
                        continue;
                    }

                    if (0 == vD.getType2()) {
                        platRate = vD.getVal();//平台手续费
                        platRate = JYMathDoubleUtils.div(platRate, 100);
                    }

                    switch (vD.getType2()) {//角色
                        case 4: {//type2 执行者4
                            switch (vD.getType3()) {//费用类型
                                case 1: { //type3  开锁 1
                                    openExeRate = vD.getVal();
                                    openExeRate = JYMathDoubleUtils.div(openExeRate, 100);
                                }
                                break;
                                case 2: { //type3  换锁 2
                                    changeExeRate = vD.getVal();
                                    changeExeRate = JYMathDoubleUtils.div(changeExeRate, 100);
                                }
                                break;
                            }
                        }
                        break;
                        case 5: {//type2  平台5
                            switch (vD.getType3()) {//费用类型
                                case 1: { //type3  开锁 1
                                    openPlatRate = vD.getVal();
                                    openPlatRate = JYMathDoubleUtils.div(openPlatRate, 100);
                                }
                                break;
                                case 2: { //type3  换锁 2
                                    changePlatRate = vD.getVal();
                                    changePlatRate = JYMathDoubleUtils.div(changePlatRate, 100);
                                }
                                break;
                            }
                        }
                        break;
                        case 41: {//type2  首发者41
                            switch (vD.getType3()) {//费用类型
                                case 1: { //type3  开锁 1
                                    openSubmitRate = vD.getVal();
                                    openSubmitRate = JYMathDoubleUtils.div(openSubmitRate, 100);
                                }
                                break;
                                case 2: { //type3  换锁 2
                                    changeSubmitRate = vD.getVal();
                                    changeSubmitRate = JYMathDoubleUtils.div(changeSubmitRate, 100);
                                }
                                break;
                            }
                        }
                        break;
                    }
                }
                extra3Sb.append("\"openMoney\":").append(openMoney).append(",");
                extra3Sb.append("\"changeMoney\":").append(changeMoney).append(",");

                //执行者
                //开锁执行者分成
                if (null != openExeRate) {
                    openExeMoney = JYMathDoubleUtils.mul(openMoney, openExeRate);
                    extra3Sb.append("\"openExeRate\":").append(openExeRate).append(",");
                    extra3Sb.append("\"openExeMoney\":").append(openExeMoney).append(",");
                }
                //换锁执行者分成
                if (null != changeExeRate) {
                    changeExeMoney = JYMathDoubleUtils.mul(changeMoney, changeExeRate);
                    extra3Sb.append("\"changeExeRate\":").append(changeExeRate).append(",");
                    extra3Sb.append("\"changeExeMoney\":").append(changeExeMoney).append(",");
                }
                //开锁首发执行者分成
                if (null != openSubmitRate) {
                    openSubmitMoney = JYMathDoubleUtils.mul(openMoney, openSubmitRate);
                    extra3Sb.append("\"openSubmitRate\":").append(openSubmitRate).append(",");
                    extra3Sb.append("\"openSubmitMoney\":").append(openSubmitMoney).append(",");
                }
                //换锁首发执行者分成
                if (null != changeSubmitRate) {
                    changeSubmitMoney = JYMathDoubleUtils.mul(changeMoney, changeSubmitRate);
                    extra3Sb.append("\"changeSubmitRate\":").append(changeSubmitRate).append(",");
                    extra3Sb.append("\"changeSubmitMoney\":").append(changeSubmitMoney).append(",");
                }

                //平台比例
                extra3Sb.append("\"platRate\":").append(platRate).append(",");
                extra3Sb.append("\"openPlatRate\":").append(openPlatRate).append(",");
                extra3Sb.append("\"changePlatRate\":").append(changePlatRate).append(",");

                //执行者最终金额及比例
                postmanMoney = JYMathDoubleUtils.add(openExeMoney, changeExeMoney);//执行者金额
                postmanRate = JYMathDoubleUtils.div(postmanMoney, payMoney);//执行者综合比例

                //首发者最终金额及比例
                shopMoney = JYMathDoubleUtils.add(openSubmitMoney, changeSubmitMoney);//首发者金额
                shopRate = JYMathDoubleUtils.div(shopMoney, payMoney);//首发者综合比例

                //平台比例
                platformRate = JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(1, postmanRate), shopRate);//平台分成比例
                //平台收入 = 支付金额-分成给执行者的金额-分给首发者的金额
                platformMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(payMoney, postmanMoney), shopMoney);
                extra3Sb.append("\"platMoney\":").append(platformMoney).append(",");

                //插入流水
                OrderPayInfo orderPayInfo = new OrderPayInfo(orderNo, orderInfo.getTotalPrice(), orderInfo.getBuyer(), orderInfo.getUsername(), -4l, payType, 1, username, 0, DateUtils.getCurrTime(),
                        shopRate, shopMoney, postmanRate, postmanMoney, platformRate, platformMoney,
                        0d, 0d);
                orderPayInfo.setBuyerAppId(payerAppId);
                orderPayInfo.setBuyerLogonId(payerUserAccount);
                extra3Sb.append("\"level\":").append(level);
                extra3Sb.append("}");
                orderPayInfo.setExtra3(extra3Sb.toString());
                orderPayInfo.setPostman(orderInfo.getPostManUsername());
                try {
                    UserDaoSql.getInstance().insertObj(PojoTables.OrderPay, orderPayInfo);
                } catch (SQLException e) {
                    e.printStackTrace();
                    return new ResultInfo("创建流水失败1");
                }
            }
        }


        //        //1.店铺分成
        //        if (null != orderInfo.getOrderType()
        //                && 0 != ORDER_TYPE.PAO_TUI - orderInfo.getOrderType()
        //                && 0 != ORDER_TYPE.SERVICE_LOCK - orderInfo.getOrderType()) {//跑腿订单
        //            if (null != shopInfo) {
        //                sellerId = shopInfo.getId();
        //                sellerUsername = shopInfo.getOwnUsername();
        //                shopRate = shopInfo.getMoneyRate();//分配比例
        //            }
        //
        //            if (null == SYS_SETTINGS.SHOP_SHARE_DELIVERY_MONEY) {
        //                System.out.println("未设置店铺是否分成配送费部分");
        //                return new ResultInfo("操作失败，请稍后再试。1");
        //            }
        //
        //            if (null != shopInfo && null != shopInfo.getIsSelfPost() && 0 == 1 - shopInfo.getIsSelfPost()) {
        //                //店铺是自配送
        //                Integer isDeliveryShare = SYS_SETTINGS.SHOP_SHARE_DELIVERY_MONEY;//店铺分成时  订单的配送费是否参与分成
        //                if (0 == 1 - isDeliveryShare) {//店铺分成时  配送费参与分成
        //                    //配送费店铺参与分成
        //                    shopTotalMoney = JYMathDoubleUtils.add(totalGoodsMoney, deliveryPrice);//订单总金额减配送费
        //                }
        //            }
        //
        //            //如果店铺有奖励金
        //            if (shopAwardMoney > 0) {
        //                shopTotalMoney = JYMathDoubleUtils.sub(shopTotalMoney, shopAwardMoney);//订单所有的钱 减去店铺奖励的金额
        //            }
        //
        //            if (ValidateUtil.isAbsDouble(shopRate)) {//单独设置了店铺分成比例
        //
        //            } else {//没有单独设置  按平台比例
        //                if (0 == 1 - orderInfo.getIsZiQu()) {//自取订单  无配送费
        ////                    shopRate = Double.parseDouble(SysSettingMemory.getInstance().getByName(SysSettingKey.SHOP_RATE_DAODIAN).getVal() + "");
        //                    if (null == SYS_SETTINGS.SHOP_RATE_DAODIAN) {
        //                        System.out.println("未设置到店消费类店铺分成比例");
        //                        return new ResultInfo("操作失败，请稍后再试。2");
        //                    }
        //                    shopRate = Double.parseDouble(SYS_SETTINGS.SHOP_RATE_DAODIAN + "");
        //                } else {//配送类订单
        ////                    shopRate = Double.parseDouble(SysSettingMemory.getInstance().getByName(SysSettingKey.SHOP_RATE_PEISONG).getVal() + "");
        //
        //                    if (null == SYS_SETTINGS.SHOP_RATE_PEISONG) {
        //                        System.out.println("未设置配送类店铺分成比例");
        //                        return new ResultInfo("操作失败，请稍后再试。3");
        //                    }
        //                    shopRate = Double.parseDouble(SYS_SETTINGS.SHOP_RATE_PEISONG + "");
        //                }
        //            }
        //
        //            if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 == 1 - SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {
        //                shopTotalMoney = JYMathDoubleUtils.add(shopTotalMoney, packetPrice);//打包费参与分成
        //            }
        //
        //            shopMoney = JYMathDoubleUtils.mul(shopTotalMoney, JYMathDoubleUtils.div(shopRate, 100));//分配金额  此时orderTotalMoney是订单所有的钱 减掉不应该有的配送费 再减掉店铺奖励的金额了
        //
        //            if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 == SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {
        //                shopMoney = JYMathDoubleUtils.add(shopMoney, packetPrice);//打包费不参与分成 直接给商家所有钱
        //            }
        //
        //        } else if (null != orderInfo.getOrderType()
        //                && 0 == ORDER_TYPE.SERVICE_LOCK - orderInfo.getOrderType()) {//跑腿订单
        //            shopTotalMoney = 0d;
        //        }
        //
        //        //2. 配送员部分
        //        if (0 == orderInfo.getIsZiQu() //配送的订单才进来
        //                || (null != orderInfo.getOrderType() && 0 == ORDER_TYPE.SERVICE_LOCK - orderInfo.getOrderType())) {//或锁匠服务订单
        //            if (null == SYS_SETTINGS.POSTMAN_SHARE_MONEY) {
        //                System.out.println("未设置配送员是否参与分成");
        //                return new ResultInfo("操作失败，请稍后再试。4");
        //            }
        //
        //            //如果店铺是自配送的  则无需再处理配送费部分
        //            if ((null != shopInfo && null != shopInfo.getIsSelfPost() && 0 != 1 - shopInfo.getIsSelfPost()) //店铺是非自配送
        //                    || (null != orderInfo.getOrderType() && 0 == ORDER_TYPE.SERVICE_LOCK - orderInfo.getOrderType())) {//或者是锁匠服务单
        //
        //                Integer postManShare = SYS_SETTINGS.POSTMAN_SHARE_MONEY; //配送员是否参与分成（0不参与，1总金额，2参与配送费部分）
        //                switch (postManShare) {
        //                    case SysSettingValueKey.POSTMAN_SHARE_NONE: {//不参与分成
        //                        postmanRate = 0d;
        //                    }
        //                    break;
        //                    case SysSettingValueKey.POSTMAN_SHARE_ALL: {//参与订单总金额分成
        //                        Double totalMoney = orderInfo.getTotalMoney();//订单总金额
        //                        //配送员参与分成比例
        ////                        postmanRate = Double.parseDouble(SysSettingMemory.getInstance().getByName(SysSettingKey.POSTMAN_RATE).getVal() + "");//配送员分成比例
        //
        //                        if (null == SYS_SETTINGS.POSTMAN_RATE) {
        //                            System.out.println("未设置配送员分成比例");
        //                            return new ResultInfo("操作失败，请稍后再试。5");
        //                        }
        //                        postmanRate = Double.parseDouble(SYS_SETTINGS.POSTMAN_RATE + "");//配送员分成比例
        //                        if (ValidateUtil.isNotNull(orderInfo.getPostManUsername())) {
        //                            CityPostman cityPostman = CityPostmanMemory.getInstance().getPostman(orderInfo.getPostManUsername());
        //                            if (null != cityPostman && ValidateUtil.isAbsDouble(cityPostman.getMoneyRate())) {
        //                                postmanRate = cityPostman.getMoneyRate();
        //                            }
        //                        }
        //
        //                        if (ValidateUtil.isAbsDouble(postmanRate)) {
        //                            postmanMoney = JYMathDoubleUtils.mul(totalMoney, JYMathDoubleUtils.div(postmanRate, 100));
        //                        }
        //                    }
        //                    break;
        //                    case SysSettingValueKey.POSTMAN_SHARE_DELIVERY: {//参与配送费部分
        //                        if (null == SYS_SETTINGS.POSTMAN_RATE) {
        //                            System.out.println("未设置配送员分成比例");
        //                            return new ResultInfo("操作失败，请稍后再试。5");
        //                        }
        //                        postmanRate = Double.parseDouble(SYS_SETTINGS.POSTMAN_RATE + "");//配送员分成比例
        //                        if (ValidateUtil.isNotNull(orderInfo.getPostManUsername())) {
        //                            CityPostman cityPostman = CityPostmanMemory.getInstance().getPostman(orderInfo.getPostManUsername());
        //                            if (null != cityPostman && ValidateUtil.isAbsDouble(cityPostman.getMoneyRate())) {
        //                                postmanRate = cityPostman.getMoneyRate();
        //                            }
        //                        }
        //
        //                        if (ValidateUtil.isAbsDouble(postmanRate)) {
        //                            postmanMoney = JYMathDoubleUtils.mul(deliveryPrice, JYMathDoubleUtils.div(postmanRate, 100));
        //                        }
        //                    }
        //                    break;
        //                }
        //            }
        //
        //
        //        }
        //
        //        //3. 平台分成金额
        //        Double platformRate = JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(100, shopRate), postmanRate);//平台分成比例
        //        //平台收入 = 支付金额-分成给店铺-分成给配送员
        //        Double platformMoney = JYMathDoubleUtils.sub(JYMathDoubleUtils.sub(payMoney, shopTotalMoney), postmanMoney);
        //        if (null != SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY && 0 == SYS_SETTINGS.SHARE_SHOP_PACKET_MONEY) {//打包费全给商家
        //            platformMoney = JYMathDoubleUtils.sub(platformMoney, packetPrice);//给商家了 所以减掉打包费
        //        }
        //
        //        OrderPayInfo orderPayInfo = new OrderPayInfo(orderNo, orderInfo.getTotalPrice(), orderInfo.getUsername(), sellerUsername, sellerId, payType, 1, username, 0, DateUtils.getCurrTime(),
        //                JYMathDoubleUtils.div(shopRate, 100), shopMoney, JYMathDoubleUtils.div(postmanRate, 100), postmanMoney, JYMathDoubleUtils.div(platformRate, 100), platformMoney,
        //                shopAwardMoney, platformAwardMoney);
        //        orderPayInfo.setBuyerAppId(payerAppId);
        //        orderPayInfo.setBuyerLogonId(payerUserAccount);
        //
        //        try {
        //            UserDaoSql.getInstance().insertObj(PojoTables.OrderPay, orderPayInfo);
        //        } catch (SQLException e) {
        //            e.printStackTrace();
        //            return new ResultInfo("创建流水失败");
        //        }
        //缓存流水
        OrderPayMemory.getInstance().add(orderNo);// 开锁服务单
        try {
            JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.ORDER_PAY_ADD).setStr01(orderNo);
            JmsSender.setTopicSender(notify);
        } catch (Exception e) {
        }
        return new ResultInfo();
    }

    //更新锁匠积分等级
    private static boolean updatePostmanLevel(String orderNo, Double money, String postmanUsername, String
            opUsername, Integer orderType) {
        //
        if (ValidateUtil.isNotAbsDouble(money)) {
            return true;
        }
        CityPostman cityPostman = CityPostmanMemory.getInstance().getPostman(postmanUsername);
        if (null == cityPostman) {
            return true;
        }
        Double score = cityPostman.getScore();
        if (null == score) {
            score = 0d;
        }
        Double nScore = money;
        if (null != SYS_SETTINGS.POSTMAN_MONEY_SCORE && SYS_SETTINGS.POSTMAN_MONEY_SCORE > 0) {
            nScore = JYMathDoubleUtils.div(money, SYS_SETTINGS.POSTMAN_MONEY_SCORE);
        }

        score = JYMathDoubleUtils.add(score, nScore);
        cityPostman.setScore(score);
        List<SysVariableScoreLevel> list = SysVariableScoreLevelMemory.getInstance().listOfUserType(UserType.POSTMAN);
        JYListSort.sort(list, "rang");
        if (ValidateUtil.isAbsList(list)) {
            Integer level = 1;
            for (int i = 0; i < list.size(); i++) {
                SysVariableScoreLevel info = list.get(i);
                if (null == info) {
                    continue;
                }
                Double rang = info.getRang();
                if (null == rang) {
                    continue;
                }
                //                if (score - rang < 0) {
                //                    level = info.getLevel();
                //                    break;
                //                }
                if (JYMathDoubleUtils.sub(score, rang) < 0) {
                    level = info.getLevel() - 1;
                    break;
                }

            }
            cityPostman.setLevel(level);
        }

        //更新积分及等级
        StringBuffer sb = new StringBuffer();
        sb.append("update ").append(PojoTables.CityPostman).append(" set score=").append(cityPostman.getScore()).append(",level=")
                .append(cityPostman.getLevel()).append(",updateUser='").append(opUsername).append("' where delFlag = 0 and username='").append(postmanUsername).append("'");
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            System.out.println("更新锁匠积分失败" + e.getMessage());
            e.printStackTrace();
            return false;
        }


        //添加用户积分流水
        UserScoreFlow flow = new UserScoreFlow();
        flow.setOrderNo(orderNo);
        flow.setOrderTotalMoney(money);
        flow.setScore(nScore);
        flow.setCreateUser(opUsername);
        flow.setDelFlag(0);
        flow.setCreateTime(DateUtils.getCurrTime());
        flow.setFlowType(UserWalletFlowType.FLOW_INCOME);
        flow.setOrderType(orderType);
        flow.setUsername(postmanUsername);
        flow.setUserType(UserType.POSTMAN);
        flow.setIsRefund(0);

        try {
            UserDaoSql.getInstance().insertObj(PojoTables.USER_SCORE_FLOW, flow);
        } catch (SQLException e) {
            System.out.println("插入积分流水失败：" + e.getMessage());
            e.printStackTrace();
            return false;
        }

        return true;
    }



    // ---------------聚合订单相关操作问题-----------------
    // 聚合订单 支付完成后 修改订单状态
    public static ResultInfo modifyMultipleOrderStatus(String orderNo, StringBuffer payType2Str) {
        // 订单编号
        if (ValidateUtil.isNull(orderNo)) {
            return new ResultInfo();
        }
        HashSet<String> hashSet = OrderInfoMemory.getInstance().getOrderNoByPayNo(orderNo);
        if (null == hashSet || hashSet.size() <= 0) {
            return new ResultInfo();
        }
        for (String oNo : hashSet) {
            OrderInfo childOrder = OrderInfoMemory.getInstance().getOrderInfo(oNo);
            if (childOrder == null) {
                continue;
            }
            //如果是自取的话生成取货验证码
            String verifyCode = "";
            try {
                if (0 == 1 - childOrder.getIsZiQu()) {
                    verifyCode = ",verifyCode='" + OrderMan.getVerifyCodeDaodian() + "'";
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (ValidateUtil.isNull(verifyCode)) {
                verifyCode = "";
            }
            if (OrderMan.isMultipleParentOrder(childOrder)) {
                verifyCode = "";
            }

            // 修改店铺的每日单号
            String orderPreNo = "";
            if ((null == childOrder.getPreDayNo() || 0 == childOrder.getPreDayNo()) && null != childOrder.getShopId() && null != childOrder.getOrderType()) {
                String shopOwn = "admin";
                ShopInfo shopInfo = ShopMemory.getInstance().getShopById(childOrder.getShopId());
                if (null != shopInfo) {
                    shopOwn = shopInfo.getOwnUsername();
                }
                int preDayNo = SubmitOrderMan.getShopPreDayNo(childOrder.getShopId(), shopOwn, childOrder.getUsername());
                orderPreNo = ",preDayNo=" + preDayNo;
            }

            StringBuilder sb = new StringBuilder();
            sb.append("update ").append(PojoTables.OrderInfo)
                    .append(" set isPaid =1,orderStatus=").append(ORDER_STATUS.PAYED)
                    .append(verifyCode)
                    .append(orderPreNo)
                    .append(payType2Str.toString())
                    .append(" where delFlag =0 and  orderNo = '").append(oNo).append("'");

            Long time = DateUtils.getCurrTime();
            OrderStatus orderStatus = new OrderStatus(oNo, ORDER_STATUS.PAYED, childOrder.getUsername(), time);
            //已成功支付，更新订单状态
            try {
                UserDaoSql.getInstance().executeBySql(sb.toString());
                UserDaoSql.getInstance().insertObj(PojoTables.OrderStatus, orderStatus);
            } catch (SQLException e) {
                e.printStackTrace();
                return new ResultInfo("更新订单状态失败，请稍后再试");
            }
            //更新缓存中订单状态
            OrderInfoMemory.getInstance().modifyStatus(oNo);
            OrderStatusMemory.getInstance().addStatus(oNo);
        }
        return new ResultInfo();
    }

    // 聚合订单 极光推送给店铺
    public static boolean multipleOrderNotice(String sysAppKey, OrderInfo orderInfo) {
        // 订单编号
        if (OrderMan.isMultipleParentOrder(orderInfo)) {
            String orderNo = orderInfo.getOrderNo();
            HashSet<String> hashSet = OrderInfoMemory.getInstance().listOfParentOrderNo(orderNo);
            for (String oNo : hashSet) {
                OrderInfo childOrder = OrderInfoMemory.getInstance().getOrderInfo(oNo);
                if (childOrder == null) {
                    continue;
                }
                if (childOrder.getShopId() != null) {
                    ShopInfo shopInfo = ShopMemory.getInstance().getShopById(childOrder.getShopId());
                    if (shopInfo != null) {
                        Notice.publishNotice2Person(sysAppKey, shopInfo.getOwnUsername(), JPushMessage.SHOP_OWNER, NoticeType.order, "又有新的订单了", "有新的订单，请尽快处理");
                    }
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {

        //        ResultInfo resultInfo = getTradeStatusQ("wx_xcx", "1803062316139461", "");
        //        if (null == resultInfo) {
        //            System.out.println("请稍后重试");
        //        } else {
        //            if (!resultInfo.isSuccess()) {//失败，返回错误
        //                System.out.println("订单支付成功后，查询订单失败1");
        //            }
        //
        //        }
        //        System.out.println(JYMathDoubleUtils.mul(29.0000d, 0.83d));


        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setUsername("15969697446");
        orderInfo.setOrderNo("1811161510438060");
        orderInfo.setTotalPrice(1d);
        String requrstStr = HttpRequest.sendPost("https://jycloud.jinyouapp.com/api/distribute/order/commision/pay"
                , "username=" + orderInfo.getUsername() + "&orderNo=" + orderInfo.getOrderNo() + "&orderMoney=" +
                        orderInfo.getTotalPrice() + "&sysCustomer=o2o-deng5&shopId=57&shopName=店铺&shopOwner=" + "你好" + "&user_name=朱文祥&user_phone=" + orderInfo.getTelephone() + "&note=" + orderInfo.getNote());
        DeviceCompanyContact companyContact = new Gson().fromJson(requrstStr, DeviceCompanyContact.class);

    }


    synchronized public static ResultInfo distributeYYMoneyMulti(OrderInfo orderInfo, String orderNo, String
            opUsername) {
        if (ValidateUtil.isNull(orderNo)) {
            return new ResultInfo("订单信息有误");
        }
        // 是否聚合订单
        if (!OrderMan.isMultipleParentOrder(orderInfo)) {
            return new ResultInfo();
        }
        // 货到付款没有分成
        if (null != orderInfo && null != orderInfo.getIsArrivePay() && orderInfo.getIsArrivePay() == 1) {
            return new ResultInfo();
        }

        OrderPayInfo orderPayInfo = OrderPayMemory.getInstance().get(orderNo);
        if (null == orderPayInfo) {
            return new ResultInfo("订单信息有误2.");
        }
        Connection cn = null;
        try {
            cn = UserDaoSql.getInstance().getConnection(true);
        } catch (SQLException e) {
            e.printStackTrace();
            return new ResultInfo("操作失败,请稍后重试");
        }
        // 主订单分成骑手和平台的
        //是否已经添加过流水，添加过后不再添加
        if (!UserWalletFlowMemory.getInstance().hasFlow(orderNo, UserWalletFlowType.FLOW_INCOME)) {
            if (null != orderInfo && null != orderInfo.getOrderType() && (
                    0 == ORDER_TYPE.SHOP_GOODS - orderInfo.getOrderType()//外卖订单
                            || 0 == ORDER_TYPE.SHOP_ACTIVITY - orderInfo.getOrderType()//美羹 活动订单
                            || 0 == ORDER_TYPE.SHARE_GOODS_ORDER - orderInfo.getOrderType()//爱甩卖 分享商品购买订单
                            || 0 == ORDER_TYPE.PAO_TUI - orderInfo.getOrderType()
                            || 0 == ORDER_TYPE.DAI_GOU - orderInfo.getOrderType()
                            || 0 == ORDER_TYPE.BANG_BAN - orderInfo.getOrderType()
                            || 0 == ORDER_TYPE.HUO_YUN - orderInfo.getOrderType())) {

                //2. 配送员
                // 2. 配送员分成
                // 骑手
                if (ValidateUtil.isNotNull(orderPayInfo.getPostman())) {
                    Double postmanMoney = JYMathDoubleUtils.add(orderPayInfo.getPostmanMoney(), orderPayInfo.getPlateAllowance());
                    postmanMoney = JYMathDoubleUtils.add(postmanMoney, orderPayInfo.getCompanyAllowance());
                    UserWalletFlow postmanFlow = new UserWalletFlow(orderPayInfo.getPostman(), UserType.POSTMAN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                            postmanMoney, null, UserWalletFlowType.FLOW_INCOME,
                            opUsername, DateUtils.getCurrTime());
                    //汇率
                    if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                        postmanFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                        postmanFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                        postmanFlow.setFcMoney(JYMathDoubleUtils.mul(postmanMoney, SYS_SETTINGS.EXCHANGE_RATE));
                    }
                    try {
                        UserDaoSql.getInstance().saveObj(PojoTables.USER_WALLET_FLOW, postmanFlow, cn);//分成 配送流水
                    } catch (SQLException e) {
                        e.printStackTrace();
                        UserDaoSql.getInstance().rollback(cn);
                        UserDaoSql.getInstance().close(cn);
                        return new ResultInfo("创建配送员分配流水失败.");
                    }
                    if (ValidateUtil.isAbsDouble(postmanMoney)) {
                        try {
                            PostManSalaryService.getInstance().addSalary2User(orderPayInfo.getPostman(), orderPayInfo.getPostCompanyId(), orderPayInfo.getPostmanMoney(), JYMathDoubleUtils.add(orderPayInfo.getPlateAllowance(), orderPayInfo.getCompanyAllowance()), cn);
                        } catch (Exception e) {
                            e.printStackTrace();
                            UserDaoSql.getInstance().rollback(cn);
                            UserDaoSql.getInstance().close(cn);
                            System.out.println("增加骑手账户失败");
                            return new ResultInfo("增加骑手账户失败");
                        }
                    }
                }
                //  2.2 配送公司对应的骑手分成
                if (ValidateUtil.isID(orderPayInfo.getPostCompanyId())) {
                    Double companyMoney = JYMathDoubleUtils.add(orderPayInfo.getPostCompanyMoney(), orderPayInfo.getPlateAllowance());
                    // 第二种计算方式 将配送员的钱给公司记录上
                    companyMoney = JYMathDoubleUtils.add(companyMoney, orderPayInfo.getPostmanMoney());
                    System.out.println();
                    PostCompanyInfo companyInfo = PostCompanyMemory.getInstance().getById(orderPayInfo.getPostCompanyId());
                    String postCompanyUsername = null;
                    if (companyInfo == null) {
                        System.out.println("未获取到配送公司信息");
                    } else {
                        postCompanyUsername = companyInfo.getUsername();
                    }
                    if (!ValidateUtil.isNull(postCompanyUsername)) {
                        UserWalletFlow postmanFlow = new UserWalletFlow(postCompanyUsername, UserType.POST_COMPANY, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                                companyMoney, null, UserWalletFlowType.FLOW_INCOME,
                                opUsername, DateUtils.getCurrTime());
                        //汇率
                        if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                            postmanFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                            postmanFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                            postmanFlow.setFcMoney(JYMathDoubleUtils.mul(companyMoney, SYS_SETTINGS.EXCHANGE_RATE));
                        }
                        try {
                            UserDaoSql.getInstance().saveObj(PojoTables.USER_WALLET_FLOW, postmanFlow, cn);//分成 配送流水
                        } catch (SQLException e) {
                            e.printStackTrace();
                            UserDaoSql.getInstance().rollback(cn);
                            UserDaoSql.getInstance().close(cn);
                            return new ResultInfo("创建公司流水失败");
                        }
                    }
                    ResultInfo resultInfoPostCompany = UserWalletMan.saveOrUpdateUserWallet(postCompanyUsername, UserType.POST_COMPANY, null, UserWalletMan.ADD, companyMoney, 0d, opUsername);
                    if (null != resultInfoPostCompany && !resultInfoPostCompany.isSuccess()) {
                        System.out.println("更新配送公司余额失败:" + resultInfoPostCompany.getError());
                        UserDaoSql.getInstance().rollback(cn);
                        UserDaoSql.getInstance().close(cn);
                        return resultInfoPostCompany;
                    }
                }

                // 创业广场
                if (ValidateUtil.isNotNull(orderPayInfo.getSquareUsername())) {
                    Double squareRate = orderPayInfo.getSquareRate();
                    Double squareMoney = orderPayInfo.getSquareMoney();
                    UserWalletFlow businessFlow = new UserWalletFlow(orderPayInfo.getBusiness(), UserType.SHOP_OWNER, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                            squareMoney, squareRate, UserWalletFlowType.FLOW_INCOME,
                            opUsername, DateUtils.getCurrTime());

                    //汇率
                    if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                        businessFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                        businessFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                        businessFlow.setFcMoney(JYMathDoubleUtils.mul(squareMoney, SYS_SETTINGS.EXCHANGE_RATE));
                    }

                    try {
                        UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, businessFlow);//
                    } catch (SQLException e) {
                        e.printStackTrace();
                        UserDaoSql.getInstance().rollback(cn);
                        UserDaoSql.getInstance().close(cn);
                        return new ResultInfo("创建业务员分配流水失败.");
                    }

                    if (squareMoney != null) {
                        //业务员余额增加
                        ResultInfo businessResult = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getBusiness(), UserType.SHOP_OWNER, null, UserWalletMan.ADD, squareMoney, 0d, opUsername);
                        if (null != businessResult && !businessResult.isSuccess()) {
                            System.out.println("更新业务员余额错误:" + businessResult.getError());
                        }
                    }
                }
                //3. 业务员 add by zhuwx 2019-2-12
                if (ValidateUtil.isNotNull(orderPayInfo.getBusiness())) {
                    Double businessRate = orderPayInfo.getBusinessRate();
                    Double businessMoney = orderPayInfo.getBusinessMoney();
                    UserWalletFlow businessFlow = new UserWalletFlow(orderPayInfo.getBusiness(), UserType.BUSINESS_MAN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                            businessMoney, businessRate, UserWalletFlowType.FLOW_INCOME,
                            opUsername, DateUtils.getCurrTime());

                    //汇率
                    if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                        businessFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                        businessFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                        businessFlow.setFcMoney(JYMathDoubleUtils.mul(businessMoney, SYS_SETTINGS.EXCHANGE_RATE));
                    }

                    try {
                        UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, businessFlow);//分成 配送流水
                    } catch (SQLException e) {
                        e.printStackTrace();
                        UserDaoSql.getInstance().rollback(cn);
                        UserDaoSql.getInstance().close(cn);
                        return new ResultInfo("创建业务员分配流水失败.");
                    }

                    if (ValidateUtil.isAbsDouble(businessMoney)) {
                        //业务员余额增加
                        ResultInfo businessResult = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getBusiness(), UserType.BUSINESS_MAN, null, UserWalletMan.ADD, businessMoney, 0d, opUsername);
                        if (null != businessResult && !businessResult.isSuccess()) {
                            System.out.println("更新业务员余额错误:" + businessResult.getError());
                        }
                    }
                }

                //4. 城代 add by zhuwx 2019-2-12
                if (ValidateUtil.isNotNull(orderPayInfo.getCityAdmin())) {
                    Double cityAdminRate = orderPayInfo.getCityAdminRate();
                    Double cityAdminMoney = orderPayInfo.getCityAdminMoney();
                    UserWalletFlow cityAdminFlow = new UserWalletFlow(orderPayInfo.getCityAdmin(), UserType.ADMIN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                            cityAdminMoney, cityAdminRate, UserWalletFlowType.FLOW_INCOME,
                            opUsername, DateUtils.getCurrTime());

                    //汇率
                    if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                        cityAdminFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                        cityAdminFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                        cityAdminFlow.setFcMoney(JYMathDoubleUtils.mul(cityAdminMoney, SYS_SETTINGS.EXCHANGE_RATE));
                    }

                    try {
                        UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, cityAdminFlow);//分成 配送流水
                    } catch (SQLException e) {
                        e.printStackTrace();
                        UserDaoSql.getInstance().rollback(cn);
                        UserDaoSql.getInstance().close(cn);
                        return new ResultInfo("创建城代分配流水失败.");
                    }

                    if (ValidateUtil.isAbsDouble(cityAdminMoney)) {
                        //业务员余额增加
                        ResultInfo cityAdminResult = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getCityAdmin(), UserType.ADMIN, null, UserWalletMan.ADD, cityAdminMoney, 0d, opUsername);
                        if (null != cityAdminResult && !cityAdminResult.isSuccess()) {
                            System.out.println("更新城代余额错误:" + cityAdminResult.getError());
                        }
                    }
                }

                // 直系代理
                if (ValidateUtil.isNotNull(orderPayInfo.getCityAdmin1())) {
                    String cityAdmin = orderPayInfo.getCityAdmin1();
                    Double cityAdminRate = orderPayInfo.getCityAdminRate1();
                    Double cityAdminMoney = orderPayInfo.getCityAdminMoney1();
                    if (cityAdminMoney != 0.0d && ValidateUtil.isAbsDouble(cityAdminRate)) {
                        UserWalletFlow cityAdminFlow = new UserWalletFlow(cityAdmin, UserType.ADMIN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                                cityAdminMoney, cityAdminRate, UserWalletFlowType.FLOW_INCOME,
                                opUsername, DateUtils.getCurrTime());

                        //汇率
                        if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                            cityAdminFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                            cityAdminFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                            cityAdminFlow.setFcMoney(JYMathDoubleUtils.mul(cityAdminMoney, SYS_SETTINGS.EXCHANGE_RATE));
                        }

                        try {
                            UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, cityAdminFlow);//分成 配送流水
                        } catch (SQLException e) {
                            e.printStackTrace();
                            UserDaoSql.getInstance().rollback(cn);
                            UserDaoSql.getInstance().close(cn);
                            return new ResultInfo("创建城代分配流水失败.");
                        }
                        // 添加统计流水

                        if (cityAdminMoney != null && cityAdminMoney != 0.0d) {
                            //业务员余额增加
                            ResultInfo cityAdminResult = UserWalletMan.saveOrUpdateUserWallet(cityAdmin, UserType.ADMIN, null, UserWalletMan.ADD, cityAdminMoney, 0d, opUsername);
                            if (null != cityAdminResult && !cityAdminResult.isSuccess()) {
                                System.out.println("更新城代余额错误:" + cityAdminResult.getError());
                            }
                        }


                    }
                }

                // 二级代理分成信息
                if (ValidateUtil.isNotNull(orderPayInfo.getCityAdmin2())) {
                    String cityAdmin = orderPayInfo.getCityAdmin2();
                    Double cityAdminRate = orderPayInfo.getCityAdminRate2();
                    Double cityAdminMoney = orderPayInfo.getCityAdminMoney2();
                    if (cityAdminMoney != 0.0d && ValidateUtil.isAbsDouble(cityAdminRate)) {
                        UserWalletFlow cityAdminFlow = new UserWalletFlow(cityAdmin, UserType.ADMIN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                                cityAdminMoney, cityAdminRate, UserWalletFlowType.FLOW_INCOME,
                                opUsername, DateUtils.getCurrTime());

                        //汇率
                        if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                            cityAdminFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                            cityAdminFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                            cityAdminFlow.setFcMoney(JYMathDoubleUtils.mul(cityAdminMoney, SYS_SETTINGS.EXCHANGE_RATE));
                        }

                        try {
                            UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, cityAdminFlow);//分成 配送流水
                        } catch (SQLException e) {
                            e.printStackTrace();
                            UserDaoSql.getInstance().rollback(cn);
                            UserDaoSql.getInstance().close(cn);
                            return new ResultInfo("创建城代分配流水失败.");
                        }
                        if (null != cityAdminMoney && cityAdminMoney != 0.0d) {
                            //业务员余额增加
                            ResultInfo cityAdminResult = UserWalletMan.saveOrUpdateUserWallet(cityAdmin, UserType.ADMIN, null, UserWalletMan.ADD, cityAdminMoney, 0d, opUsername);
                            if (null != cityAdminResult && !cityAdminResult.isSuccess()) {
                                System.out.println("更新城代余额错误:" + cityAdminResult.getError());
                            }
                        }
                    }
                }

                // 三级代理
                if (ValidateUtil.isNotNull(orderPayInfo.getCityAdmin3())) {
                    String cityAdmin = orderPayInfo.getCityAdmin3();
                    Double cityAdminRate = orderPayInfo.getCityAdminRate3();
                    Double cityAdminMoney = orderPayInfo.getCityAdminMoney3();
                    if (cityAdminMoney != 0.0d && ValidateUtil.isAbsDouble(cityAdminRate)) {
                        UserWalletFlow cityAdminFlow = new UserWalletFlow(cityAdmin, UserType.ADMIN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                                cityAdminMoney, cityAdminRate, UserWalletFlowType.FLOW_INCOME,
                                opUsername, DateUtils.getCurrTime());

                        //汇率
                        if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                            cityAdminFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                            cityAdminFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                            cityAdminFlow.setFcMoney(JYMathDoubleUtils.mul(cityAdminMoney, SYS_SETTINGS.EXCHANGE_RATE));
                        }

                        try {
                            UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, cityAdminFlow);//分成 配送流水
                        } catch (SQLException e) {
                            e.printStackTrace();
                            UserDaoSql.getInstance().rollback(cn);
                            UserDaoSql.getInstance().close(cn);
                            return new ResultInfo("创建城代分配流水失败.");
                        }
                        if (cityAdminMoney != null && cityAdminMoney != 0.0d) {
                            //业务员余额增加
                            ResultInfo cityAdminResult = UserWalletMan.saveOrUpdateUserWallet(cityAdmin, UserType.ADMIN, null, UserWalletMan.ADD, cityAdminMoney, 0d, opUsername);
                            if (null != cityAdminResult && !cityAdminResult.isSuccess()) {
                                System.out.println("更新城代余额错误:" + cityAdminResult.getError());
                            }
                        }
                    }
                }
                // 四级代理分成
                if (ValidateUtil.isNotNull(orderPayInfo.getCityAdmin4())) {
                    String cityAdmin = orderPayInfo.getCityAdmin4();
                    Double cityAdminRate = orderPayInfo.getCityAdminRate4();
                    Double cityAdminMoney = orderPayInfo.getCityAdminMoney4();
                    if (cityAdminMoney != 0.0d && ValidateUtil.isAbsDouble(cityAdminRate)) {
                        UserWalletFlow cityAdminFlow = new UserWalletFlow(cityAdmin, UserType.ADMIN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                                cityAdminMoney, cityAdminRate, UserWalletFlowType.FLOW_INCOME,
                                opUsername, DateUtils.getCurrTime());

                        //汇率
                        if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                            cityAdminFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                            cityAdminFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                            cityAdminFlow.setFcMoney(JYMathDoubleUtils.mul(cityAdminMoney, SYS_SETTINGS.EXCHANGE_RATE));
                        }

                        try {
                            UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, cityAdminFlow);//分成 配送流水
                        } catch (SQLException e) {
                            e.printStackTrace();
                            UserDaoSql.getInstance().rollback(cn);
                            UserDaoSql.getInstance().close(cn);
                            return new ResultInfo("创建城代分配流水失败.");
                        }
                        if (null != cityAdminMoney && cityAdminMoney != 0.0d) {
                            //业务员余额增加
                            ResultInfo cityAdminResult = UserWalletMan.saveOrUpdateUserWallet(cityAdmin, UserType.ADMIN, null, UserWalletMan.ADD, cityAdminMoney, 0d, opUsername);
                            if (null != cityAdminResult && !cityAdminResult.isSuccess()) {
                                System.out.println("更新城代余额错误:" + cityAdminResult.getError());
                            }
                        }
                    }
                }

                //5. 平台
                Double platformRate = orderPayInfo.getPlatformRate();
                Double platformMoney = orderPayInfo.getPlatformMoney();
                UserWalletFlow systemFlow = new UserWalletFlow("admin", UserType.SUPER_ADMIN, orderNo, orderInfo.getOrderType(), orderPayInfo.getTotalPrice(),
                        platformMoney, platformRate, UserWalletFlowType.FLOW_INCOME,
                        opUsername, DateUtils.getCurrTime());
                //汇率
                if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                    systemFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                    systemFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                    systemFlow.setFcMoney(JYMathDoubleUtils.mul(platformMoney, SYS_SETTINGS.EXCHANGE_RATE));
                }
                try {
                    UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, systemFlow);//分成 平台流水
                    UserDaoSql.getInstance().rollback(cn);
                    UserDaoSql.getInstance().close(cn);
                } catch (SQLException e) {
                    e.printStackTrace();
                    return new ResultInfo("创建系统分配流水失败.");
                }
            }
            //缓存中新增
            UserWalletFlowMemory.getInstance().addFLow(orderNo);
            try {
                JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.USER_WALLET_FLOW_ADD).setStr01(orderNo);
                JmsSender.setTopicSender(notify);
            } catch (Exception e) {
            }
        }


        // 子订单 分成店铺的
        HashSet<String> childSet = OrderInfoMemory.getInstance().listOfParentOrderNo(orderNo);
        if (null == childSet || childSet.size() < 1) {
            childSet = new HashSet<>();
        }
        for (String childOrderNo : childSet) {
            if (ValidateUtil.isNull(childOrderNo)) {
                continue;
            }
            //是否已经添加过流水，添加过后不再添加
            if (UserWalletFlowMemory.getInstance().hasFlow(childOrderNo, UserWalletFlowType.FLOW_INCOME)) {
                continue;
            }

            OrderPayInfo orderPay = OrderPayMemory.getInstance().get(childOrderNo);
            if (null == orderPay) {
                continue;
            }
            OrderInfo childOrder = OrderInfoMemory.getInstance().getOrderInfo(childOrderNo);
            if (null == childOrder) {
                continue;
            }

            // 分成
            if (null != childOrder && null != childOrder.getOrderType() && (
                    0 == ORDER_TYPE.SHOP_GOODS - childOrder.getOrderType()//外卖订单
                            || 0 == ORDER_TYPE.SHOP_ACTIVITY - childOrder.getOrderType()//美羹 活动订单
                            || 0 == ORDER_TYPE.SHARE_GOODS_ORDER - childOrder.getOrderType()//爱甩卖 分享商品购买订单
                            || 0 == ORDER_TYPE.PAO_TUI - childOrder.getOrderType()
                            || 0 == ORDER_TYPE.DAI_GOU - childOrder.getOrderType()
                            || 0 == ORDER_TYPE.BANG_BAN - childOrder.getOrderType()
                            || 0 == ORDER_TYPE.HUO_YUN - childOrder.getOrderType())) {
                //1. 店铺
                if ((null != childOrder.getOrderType() && (0 == ORDER_TYPE.SHOP_GOODS - childOrder.getOrderType() || 0 == ORDER_TYPE.SHARE_GOODS_ORDER - childOrder.getOrderType()))
                        && ValidateUtil.isNotNull(orderPayInfo.getSeller())) {
                    Double shopRate = orderPayInfo.getShopRate();//分成比例
                    Double shopMoney = orderPayInfo.getShopMoney();//分成金额
                    UserWalletFlow shopFlow = new UserWalletFlow(orderPayInfo.getSeller(), UserType.SHOP_OWNER, childOrderNo, childOrder.getOrderType(), orderPayInfo.getTotalPrice(),
                            shopMoney, shopRate, UserWalletFlowType.FLOW_INCOME,
                            opUsername, DateUtils.getCurrTime());

                    //汇率
                    if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {//是否使用主币支付 1使用主币支付 0使用外币支付
                        shopFlow.setExchangeRate(SYS_SETTINGS.EXCHANGE_RATE);
                        shopFlow.setFcOrderTotalMoney(JYMathDoubleUtils.mul(orderPayInfo.getTotalPrice(), SYS_SETTINGS.EXCHANGE_RATE));
                        shopFlow.setFcMoney(JYMathDoubleUtils.mul(shopMoney, SYS_SETTINGS.EXCHANGE_RATE));
                    }

                    try {
                        UserDaoSql.getInstance().insertObj(PojoTables.USER_WALLET_FLOW, shopFlow);//分成 店铺流水
                    } catch (SQLException e) {
                        e.printStackTrace();
                        UserDaoSql.getInstance().rollback(cn);
                        UserDaoSql.getInstance().close(cn);
                        return new ResultInfo("创建店铺分配流水失败.");
                    }

                    //店铺余额增加  v2
                    ResultInfo resultInfo = UserWalletMan.saveOrUpdateUserWallet(orderPayInfo.getSeller(), UserType.SHOP_OWNER, orderPayInfo.getSellerId(), UserWalletMan.ADD, shopMoney, 0d, opUsername);
                    if (null != resultInfo && !resultInfo.isSuccess()) {
                        System.out.println("更新店铺余额错误:" + resultInfo.getError());
                    }
                }
                //缓存中新增
                UserWalletFlowMemory.getInstance().addFLow(childOrderNo);
                try {
                    JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.USER_WALLET_FLOW_ADD).setStr01(childOrderNo);
                    JmsSender.setTopicSender(notify);
                } catch (Exception e) {
                }
            }
        }
        try {
            cn.commit();
        } catch (Exception e) {
            e.printStackTrace();
            UserDaoSql.getInstance().rollback(cn);
            return new ResultInfo("更新余额失败");
        } finally {
            UserDaoSql.getInstance().close(cn);
        }
        return new ResultInfo();

    }


}
